this repo has no description smallweb.run
smallweb
4
fork

Configure Feed

Select the types of activity you want to include in your feed.

enhance config command with JSON output and editor support

pomdtr 416e2d4d 9697e9ad

+82 -5
+80 -4
cmd/config.go
··· 1 1 package cmd 2 2 3 3 import ( 4 + "encoding/json" 4 5 "fmt" 6 + "os" 7 + "os/exec" 8 + "path/filepath" 5 9 10 + "github.com/google/shlex" 11 + "github.com/mattn/go-isatty" 12 + "github.com/pomdtr/smallweb/utils" 6 13 "github.com/spf13/cobra" 7 14 ) 8 15 16 + func findConfigPath(rootDir string) string { 17 + for _, candidate := range []string{".smallweb/config.jsonc", ".smallweb/config.json"} { 18 + configPath := filepath.Join(rootDir, candidate) 19 + if _, err := os.Stat(configPath); err == nil { 20 + return configPath 21 + } 22 + } 23 + 24 + return filepath.Join(rootDir, ".smallweb/config.json") 25 + } 26 + 27 + func getEditorCmd(args ...string) (*exec.Cmd, error) { 28 + editorEnv, ok := os.LookupEnv("EDITOR") 29 + if !ok { 30 + return exec.Command("vi", args...), nil 31 + } 32 + 33 + editorArgs, err := shlex.Split(editorEnv) 34 + if err != nil { 35 + return nil, fmt.Errorf("failed to parse EDITOR: %w", err) 36 + } 37 + 38 + editorArgs = append(editorArgs, args...) 39 + return exec.Command(editorArgs[0], editorArgs[1:]...), nil 40 + } 41 + 9 42 func NewCmdConfig() *cobra.Command { 43 + var flags struct { 44 + json bool 45 + } 46 + 10 47 cmd := &cobra.Command{ 11 48 Use: "config <key>", 12 49 Short: "Get a configuration value", 13 - Args: cobra.ExactArgs(1), 50 + Args: cobra.MaximumNArgs(1), 14 51 RunE: func(cmd *cobra.Command, args []string) error { 15 - if args[0] == "dir" { 16 - fmt.Println(k.String("dir")) 17 - return nil 52 + if len(args) == 0 { 53 + if flags.json { 54 + configBytes, err := k.Marshal(utils.ConfigParser()) 55 + if err != nil { 56 + return err 57 + } 58 + 59 + os.Stdout.Write(configBytes) 60 + return nil 61 + } 62 + 63 + if !isatty.IsTerminal(os.Stdin.Fd()) { 64 + return fmt.Errorf("stdin is not interactive") 65 + } 66 + 67 + configPath := findConfigPath(k.String("dir")) 68 + editorCmd, err := getEditorCmd(configPath) 69 + if err != nil { 70 + return err 71 + } 72 + 73 + editorCmd.Stdin = os.Stdin 74 + editorCmd.Stdout = os.Stdout 75 + editorCmd.Stderr = os.Stderr 76 + 77 + return editorCmd.Run() 18 78 } 19 79 20 80 v := k.Get(args[0]) ··· 22 82 return fmt.Errorf("key %q not found", args[0]) 23 83 } 24 84 85 + if flags.json { 86 + encoder := json.NewEncoder(os.Stdout) 87 + encoder.SetEscapeHTML(false) 88 + if isatty.IsTerminal(os.Stdout.Fd()) { 89 + encoder.SetIndent("", " ") 90 + } 91 + 92 + if err := encoder.Encode(v); err != nil { 93 + return err 94 + } 95 + 96 + return nil 97 + } 98 + 25 99 fmt.Println(v) 26 100 return nil 27 101 }, 28 102 } 103 + 104 + cmd.Flags().BoolVar(&flags.json, "json", false, "Output as JSON") 29 105 30 106 return cmd 31 107 }
+1 -1
cmd/root.go
··· 67 67 flagProvider := posflag.Provider(cmd.Root().PersistentFlags(), ".", k) 68 68 _ = k.Load(flagProvider, nil) 69 69 70 - configPath := filepath.Join(k.String("dir"), ".smallweb", "config.json") 70 + configPath := findConfigPath(k.String("dir")) 71 71 fileProvider := file.Provider(configPath) 72 72 _ = k.Load(fileProvider, utils.ConfigParser()) 73 73 _ = k.Load(envProvider, nil)
+1
go.mod
··· 94 94 github.com/golang/protobuf v1.5.4 // indirect 95 95 github.com/google/go-cmp v0.6.0 // indirect 96 96 github.com/google/s2a-go v0.1.8 // indirect 97 + github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect 97 98 github.com/google/uuid v1.6.0 // indirect 98 99 github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect 99 100 github.com/googleapis/gax-go/v2 v2.13.0 // indirect