github.com/esnet/gdg@v0.6.1-0.20240412190737-6b6eba9c14d8/cli/tools/context.go (about) 1 package tools 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "github.com/bep/simplecobra" 8 "github.com/esnet/gdg/cli/support" 9 "github.com/esnet/gdg/internal/config" 10 "github.com/jedib0t/go-pretty/v6/table" 11 "log/slog" 12 13 "github.com/spf13/cobra" 14 "strings" 15 ) 16 17 func newContextCmd() simplecobra.Commander { 18 v := &support.SimpleCommand{ 19 NameP: "contexts", 20 CommandsList: []simplecobra.Commander{newContextClearCmd(), newListContextCmd(), 21 newContextCopy(), newShowContext(), newDeleteContext(), newContext(), newSetContext()}, 22 RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, r *support.RootCommand, args []string) error { 23 return cd.CobraCommand.Help() 24 }, 25 Short: "Manage Context configuration", 26 Long: "Manage Context configuration which allows multiple grafana configs to be used.", 27 WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) { 28 cmd.Aliases = []string{"ctx", "context"} 29 }, 30 } 31 return v 32 } 33 34 func newContextClearCmd() simplecobra.Commander { 35 return &support.SimpleCommand{ 36 NameP: "clear", 37 RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, r *support.RootCommand, args []string) error { 38 config.Config().ClearContexts() 39 slog.Info("Successfully deleted all configured contexts") 40 return nil 41 }, 42 Short: "Manage Context configuration", 43 Long: "Manage Context configuration which allows multiple grafana configs to be used.", 44 } 45 } 46 47 func newListContextCmd() simplecobra.Commander { 48 return &support.SimpleCommand{ 49 NameP: "list", 50 RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error { 51 rootCmd.TableObj.AppendHeader(table.Row{"context", "active"}) 52 contexts := config.Config().GetGDGConfig().GetContexts() 53 activeContext := config.Config().GetGDGConfig().GetContext() 54 for key := range contexts { 55 active := false 56 if key == strings.ToLower(activeContext) { 57 key = fmt.Sprintf("*%s", activeContext) 58 active = true 59 } 60 rootCmd.TableObj.AppendRow(table.Row{key, active}) 61 } 62 63 rootCmd.Render(cd.CobraCommand, contexts) 64 65 return nil 66 }, 67 Short: "List context", 68 Long: "List contexts.", 69 } 70 } 71 72 func newContextCopy() simplecobra.Commander { 73 v := &support.SimpleCommand{ 74 NameP: "copy", 75 RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, r *support.RootCommand, args []string) error { 76 src := args[0] 77 dest := args[1] 78 config.Config().CopyContext(src, dest) 79 return nil 80 }, 81 InitCFunc: func(cd *simplecobra.Commandeer, r *support.RootCommand) error { 82 cd.CobraCommand.Aliases = []string{"cp"} 83 cd.CobraCommand.Args = func(cmd *cobra.Command, args []string) error { 84 if len(args) < 2 { 85 return errors.New("requires a src and destination argument") 86 } 87 return nil 88 } 89 return nil 90 }, 91 Short: "copy context <src> <dest>", 92 Long: "copy contexts <src> <dest>", 93 } 94 95 return v 96 97 } 98 99 func newShowContext() simplecobra.Commander { 100 return &support.SimpleCommand{ 101 NameP: "show", 102 RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, r *support.RootCommand, args []string) error { 103 contextEntry := config.Config().GetGDGConfig().GetContext() 104 if len(args) > 0 && len(args[0]) > 0 { 105 contextEntry = args[0] 106 } 107 config.Config().PrintContext(contextEntry) 108 return nil 109 }, 110 Short: "show optional[context]", 111 Long: "show optional[context]", 112 } 113 } 114 115 func newDeleteContext() simplecobra.Commander { 116 return &support.SimpleCommand{ 117 NameP: "delete", 118 Short: "delete context <context>", 119 Long: "delete context <context>", 120 RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, r *support.RootCommand, args []string) error { 121 if len(args) < 1 { 122 return errors.New("requires a context argument") 123 } 124 contextEntry := args[0] 125 config.Config().DeleteContext(contextEntry) 126 slog.Info("Successfully deleted context", "context", ctx) 127 return nil 128 }, 129 InitCFunc: func(cd *simplecobra.Commandeer, r *support.RootCommand) error { 130 cd.CobraCommand.Aliases = []string{"del"} 131 return nil 132 }, 133 } 134 135 } 136 137 func newContext() simplecobra.Commander { 138 return &support.SimpleCommand{ 139 NameP: "new", 140 Short: "new <context>", 141 Long: "new <context>", 142 RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error { 143 if len(args) < 1 { 144 return errors.New("requires a context NameP") 145 } 146 contextEntry := args[0] 147 config.Config().NewContext(contextEntry) 148 return nil 149 }, 150 } 151 } 152 153 func newSetContext() simplecobra.Commander { 154 return &support.SimpleCommand{ 155 NameP: "set", 156 Short: "set <context>", 157 Long: "set <context>", 158 RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error { 159 if len(args) < 1 { 160 return errors.New("requires a context argument") 161 } 162 contextEntry := args[0] 163 config.Config().ChangeContext(contextEntry) 164 return nil 165 }, 166 } 167 }