makeshift/cmd/list.go

216 lines
5.1 KiB
Go

package cmd
import (
"encoding/json"
"fmt"
"net/http"
"os"
makeshift "git.towk2.me/towk/makeshift/pkg"
"git.towk2.me/towk/makeshift/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 http://localhost:5050 --path test
# list files using 'curl'
curl http://localhost:5050/list/test
`,
Args: cobra.NoArgs,
Short: "List all files in a remote data directory",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
setenv(cmd, "host", "MAKESHIFT_HOST")
setenv(cmd, "path", "MAKESHIFT_PATH")
setenv(cmd, "cacert", "MAKESHIFT_CACERT")
},
Run: func(cmd *cobra.Command, args []string) {
var (
host, _ = cmd.Flags().GetString("host")
path, _ = cmd.Flags().GetString("path")
cacertPath, _ = cmd.Flags().GetString("cacert")
c = client.New(host)
body []byte
output []string
err error
)
log.Debug().
Str("host", host).
Str("path", path).
Str("cacert", cacertPath).
Send()
if cacertPath != "" {
c.LoadCertificateFromPath(cacertPath)
}
// 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("host", host).
Str("path", path).
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()
},
}
var listPluginsCmd = &cobra.Command{
Use: "plugins",
Example: `
# show all plugins
makeshift list plugins
# show details for specific plugins
makeshift list plugins smd jinja2
`,
Short: "Show plugins information",
Run: func(cmd *cobra.Command, args []string) {
var (
host, _ = cmd.Flags().GetString("host")
cacertPath, _ = cmd.Flags().GetString("cacert")
c = client.New(host)
res *http.Response
query string
plugins []string
body []byte
err error
)
log.Debug().
Str("host", host).
Str("cacert", cacertPath).
Send()
if cacertPath != "" {
c.LoadCertificateFromPath(cacertPath)
}
if len(args) == 0 {
// make request to /list endpoint
res, body, err = c.MakeRequest(client.HTTPEnvelope{
Path: "/plugins",
Method: http.MethodGet,
})
handleResponseError(res, host, "/plugins", err)
err = json.Unmarshal(body, &plugins)
if err != nil {
log.Error().Err(err).
Msg("failed to unmarshal plugins")
return
}
} else {
for _, pluginName := range args {
// make request to /list endpoint
query = fmt.Sprintf("/plugins/%s/info", pluginName)
res, body, err = c.MakeRequest(client.HTTPEnvelope{
Path: query,
Method: http.MethodGet,
})
handleResponseError(res, host, query, err)
plugins = append(plugins, string(body))
}
}
log.Info().Strs("plugins", plugins).Send()
},
}
var listProfilesCmd = &cobra.Command{
Use: "profiles",
Example: `
# list all profiles
makeshift list profiles
# live individual profiles
makeshift list profiles default custom
`,
Short: "Show all available profiles",
Run: func(cmd *cobra.Command, args []string) {
var (
host, _ = cmd.Flags().GetString("host")
cacertPath, _ = cmd.Flags().GetString("cacert")
c = client.New(host)
res *http.Response
profiles []makeshift.Profile
body []byte
query string
err error
)
log.Debug().
Str("host", host).
Str("cacert", cacertPath).
Send()
if cacertPath != "" {
c.LoadCertificateFromPath(cacertPath)
}
if len(args) == 0 {
// make request to /list endpoint
res, body, err = c.MakeRequest(client.HTTPEnvelope{
Path: "/profiles",
Method: http.MethodGet,
})
handleResponseError(res, host, "/profiles", err)
err = json.Unmarshal(body, &profiles)
if err != nil {
log.Error().Err(err).
Msg("failed to unmarshal plugins")
return
}
} else {
for _, profileID := range args {
// make request to /list endpoint
query = fmt.Sprintf("/profiles/%s", profileID)
res, body, err = c.MakeRequest(client.HTTPEnvelope{
Path: query,
Method: http.MethodGet,
})
handleResponseError(res, host, query, err)
var profile makeshift.Profile
err = json.Unmarshal(body, &profile)
if err != nil {
log.Error().Err(err).
Msg("failed to unmarshal plugin")
continue
}
profiles = append(profiles, profile)
}
}
log.Info().Any("plugins", profiles).Send()
},
}
func init() {
listCmd.PersistentFlags().String("host", "http://localhost:5050", "Set the configurator remote host (can be set with MAKESHIFT_HOST)")
listCmd.PersistentFlags().String("cacert", "", "Set the CA certificate path to load")
listCmd.Flags().StringP("path", "p", ".", "Set the path to list files (can be set with MAKESHIFT_PATH)")
listCmd.AddCommand(listPluginsCmd, listProfilesCmd)
rootCmd.AddCommand(listCmd)
}