misc: add --list-options for vscode extension uis (#28)

* draft dump json in help

* polish feature
This commit is contained in:
ChristopherHX
2025-01-29 17:21:54 +01:00
committed by GitHub
parent 9d516e8fa2
commit 6e7e6baa8e
3 changed files with 29 additions and 0 deletions

View File

@@ -59,6 +59,7 @@ type Input struct {
logPrefixJobID bool logPrefixJobID bool
networkName string networkName string
localRepository []string localRepository []string
listOptions bool
} }
func (i *Input) resolve(path string) string { func (i *Input) resolve(path string) string {

View File

@@ -20,6 +20,10 @@ type Notice struct {
} }
func displayNotices(input *Input) { func displayNotices(input *Input) {
// Avoid causing trouble parsing the json
if input.listOptions {
return
}
select { select {
case notices := <-noticesLoaded: case notices := <-noticesLoaded:
if len(notices) > 0 { if len(notices) > 0 {

View File

@@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"context" "context"
"encoding/json"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@@ -21,6 +22,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/cobra/doc" "github.com/spf13/cobra/doc"
"github.com/spf13/pflag"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"github.com/actions-oss/act-cli/pkg/artifactcache" "github.com/actions-oss/act-cli/pkg/artifactcache"
@@ -31,6 +33,13 @@ import (
"github.com/actions-oss/act-cli/pkg/runner" "github.com/actions-oss/act-cli/pkg/runner"
) )
type Flag struct {
Name string `json:"name"`
Default string `json:"default"`
Type string `json:"type"`
Description string `json:"description"`
}
// Execute is the entry point to running the CLI // Execute is the entry point to running the CLI
func Execute(ctx context.Context, version string) { func Execute(ctx context.Context, version string) {
input := new(Input) input := new(Input)
@@ -44,6 +53,7 @@ func Execute(ctx context.Context, version string) {
Version: version, Version: version,
SilenceUsage: true, SilenceUsage: true,
} }
rootCmd.Flags().BoolP("watch", "w", false, "watch the contents of the local repo and run when files change") rootCmd.Flags().BoolP("watch", "w", false, "watch the contents of the local repo and run when files change")
rootCmd.Flags().BoolP("list", "l", false, "list workflows") rootCmd.Flags().BoolP("list", "l", false, "list workflows")
rootCmd.Flags().BoolP("graph", "g", false, "draw workflows") rootCmd.Flags().BoolP("graph", "g", false, "draw workflows")
@@ -103,6 +113,7 @@ func Execute(ctx context.Context, version string) {
rootCmd.PersistentFlags().BoolVarP(&input.actionOfflineMode, "action-offline-mode", "", false, "If action contents exists, it will not be fetch and pull again. If turn on this, will turn off force pull") rootCmd.PersistentFlags().BoolVarP(&input.actionOfflineMode, "action-offline-mode", "", false, "If action contents exists, it will not be fetch and pull again. If turn on this, will turn off force pull")
rootCmd.PersistentFlags().StringVarP(&input.networkName, "network", "", "host", "Sets a docker network name. Defaults to host.") rootCmd.PersistentFlags().StringVarP(&input.networkName, "network", "", "host", "Sets a docker network name. Defaults to host.")
rootCmd.PersistentFlags().StringArrayVarP(&input.localRepository, "local-repository", "", []string{}, "Replaces the specified repository and ref with a local folder (e.g. https://github.com/test/test@v0=/home/act/test or test/test@v0=/home/act/test, the latter matches any hosts or protocols)") rootCmd.PersistentFlags().StringArrayVarP(&input.localRepository, "local-repository", "", []string{}, "Replaces the specified repository and ref with a local folder (e.g. https://github.com/test/test@v0=/home/act/test or test/test@v0=/home/act/test, the latter matches any hosts or protocols)")
rootCmd.PersistentFlags().BoolVar(&input.listOptions, "list-options", false, "Print a json structure of compatible options")
rootCmd.SetArgs(args()) rootCmd.SetArgs(args())
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
@@ -241,6 +252,16 @@ func generateManPage(cmd *cobra.Command) error {
return nil return nil
} }
func listOptions(cmd *cobra.Command) error {
flags := []Flag{}
cmd.LocalFlags().VisitAll(func(f *pflag.Flag) {
flags = append(flags, Flag{Name: f.Name, Default: f.DefValue, Description: f.Usage, Type: f.Value.Type()})
})
a, err := json.Marshal(flags)
fmt.Println(string(a))
return err
}
func readArgsFile(file string, split bool) []string { func readArgsFile(file string, split bool) []string {
args := make([]string, 0) args := make([]string, 0)
f, err := os.Open(file) f, err := os.Open(file)
@@ -358,6 +379,9 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
if ok, _ := cmd.Flags().GetBool("man-page"); ok { if ok, _ := cmd.Flags().GetBool("man-page"); ok {
return generateManPage(cmd) return generateManPage(cmd)
} }
if input.listOptions {
return listOptions(cmd)
}
if ret, err := container.GetSocketAndHost(input.containerDaemonSocket); err != nil { if ret, err := container.GetSocketAndHost(input.containerDaemonSocket); err != nil {
log.Warnf("Couldn't get a valid docker connection: %+v", err) log.Warnf("Couldn't get a valid docker connection: %+v", err)