forked from Elara6331/itd
Update itctl to use api
This commit is contained in:
@@ -20,8 +20,10 @@ package root
|
||||
|
||||
import (
|
||||
"github.com/abiosoft/ishell"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"go.arsenm.dev/itd/api"
|
||||
)
|
||||
|
||||
// RootCmd represents the base command when called without any subcommands
|
||||
@@ -61,18 +63,25 @@ var RootCmd = &cobra.Command{
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
client, err := api.New(viper.GetString("sockPath"))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error connecting to socket. Is itd running?")
|
||||
}
|
||||
defer client.Close()
|
||||
viper.Set("client", client)
|
||||
RootCmd.CompletionOptions.DisableDefaultCmd = true
|
||||
cobra.CheckErr(RootCmd.Execute())
|
||||
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Register flag for socket path
|
||||
RootCmd.Flags().StringP("socket-path", "s", "", "Path to itd socket")
|
||||
RootCmd.Flags().StringP("socket-path", "s", api.DefaultAddr, "Path to itd socket")
|
||||
|
||||
// Bind flag and environment variable to viper key
|
||||
viper.BindPFlag("sockPath", RootCmd.Flags().Lookup("socket-path"))
|
||||
viper.BindEnv("sockPath", "ITCTL_SOCKET_PATH")
|
||||
|
||||
// Set default value for socket path
|
||||
viper.SetDefault("sockPath", "/tmp/itd/socket")
|
||||
viper.SetDefault("sockPath", api.DefaultAddr)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user