package cmd import ( "encoding/json" "fmt" "net/http" "os" "git.towk2.me/towk/configurator/pkg/client" "github.com/rs/zerolog/log" "github.com/spf13/cobra" ) var listCmd = &cobra.Command{ Use: "list", Example: ` # list files in a remote data directory configurator list --path test configurator list --host https://example.com --path test curl https://example.com/list/test `, Args: cobra.NoArgs, Short: "List all files in a remote data directory", PreRun: func(cmd *cobra.Command, args []string) { setenv(&host, "CONFIGURATOR_HOST") setenv(&path, "CONFIGURATOR_PATH") }, Run: func(cmd *cobra.Command, args []string) { var ( c = client.New(host) body []byte output []string err error ) log.Debug(). Str("host", host). Str("path", path). Send() // make request to /list endpoint _, body, err = c.MakeRequest(client.HTTPEnvelope{ Path: fmt.Sprintf("/list/%s", path), Method: http.MethodGet, }) if err != nil { log.Error().Err(err).Str("url", host).Msg("failed to make request") os.Exit(1) } err = json.Unmarshal(body, &output) if err != nil { log.Error().Err(err).Msg("failed to unmarshal response body") os.Exit(1) } // show the list of files and directories log.Info().Strs("output", output).Send() }, } func init() { listCmd.Flags().StringVar(&host, "host", "http://localhost:5050", "Set the configurator remote host (can be set with CONFIGURATOR_HOST)") listCmd.Flags().StringVarP(&path, "path", "p", ".", "Set the path to list files (can be set with CONFIGURATOR_PATH)") rootCmd.AddCommand(listCmd) }