github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/client/cli/config/cli.go (about) 1 package config 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "strings" 7 8 "github.com/tickoalcantara12/micro/v3/client/cli/namespace" 9 "github.com/tickoalcantara12/micro/v3/client/cli/util" 10 "github.com/tickoalcantara12/micro/v3/cmd" 11 proto "github.com/tickoalcantara12/micro/v3/proto/config" 12 "github.com/tickoalcantara12/micro/v3/service/client" 13 "github.com/tickoalcantara12/micro/v3/service/context" 14 log "github.com/tickoalcantara12/micro/v3/service/logger" 15 "github.com/tickoalcantara12/micro/v3/util/helper" 16 "github.com/urfave/cli/v2" 17 ) 18 19 func setConfig(ctx *cli.Context) error { 20 args := ctx.Args() 21 // key val 22 key := args.Get(0) 23 val := args.Get(1) 24 25 pb := proto.NewConfigService("config", client.DefaultClient) 26 27 if args.Len() == 0 { 28 return cli.ShowSubcommandHelp(ctx) 29 } 30 31 env, err := util.GetEnv(ctx) 32 if err != nil { 33 return err 34 } 35 ns, err := namespace.Get(env.Name) 36 if err != nil { 37 return err 38 } 39 40 parsedVal, err := parseValue(val) 41 if err != nil { 42 return err 43 } 44 v, _ := json.Marshal(parsedVal) 45 46 // TODO: allow the specifying of a config.Key. This will be service name 47 // The actual key-val set is a path e.g micro/accounts/key 48 _, err = pb.Set(context.DefaultContext, &proto.SetRequest{ 49 // the current namespace 50 Namespace: ns, 51 // actual key for the value 52 Path: key, 53 // The value 54 Value: &proto.Value{ 55 Data: string(v), 56 //Format: "json", 57 }, 58 Options: &proto.Options{ 59 Secret: ctx.Bool("secret"), 60 }, 61 }, client.WithAuthToken()) 62 return util.CliError(err) 63 } 64 65 func parseValue(s string) (interface{}, error) { 66 var i interface{} 67 err := json.Unmarshal([]byte(s), &i) 68 if err != nil { 69 // special exception for strings 70 return i, json.Unmarshal([]byte(fmt.Sprintf("\"%v\"", s)), &i) 71 } 72 return i, nil 73 } 74 75 func getConfig(ctx *cli.Context) error { 76 args := ctx.Args() 77 78 if args.Len() == 0 { 79 return cli.ShowSubcommandHelp(ctx) 80 } 81 // key val 82 key := args.Get(0) 83 if len(key) == 0 { 84 return fmt.Errorf("key cannot be blank") 85 } 86 87 env, err := util.GetEnv(ctx) 88 if err != nil { 89 return err 90 } 91 ns, err := namespace.Get(env.Name) 92 if err != nil { 93 return err 94 } 95 96 // TODO: allow the specifying of a config.Key. This will be service name 97 // The actuall key-val set is a path e.g micro/accounts/key 98 pb := proto.NewConfigService("config", client.DefaultClient) 99 rsp, err := pb.Get(context.DefaultContext, &proto.GetRequest{ 100 // The current namespace, 101 Namespace: ns, 102 // The actual key for the val 103 Path: key, 104 Options: &proto.Options{ 105 Secret: ctx.Bool("secret"), 106 }, 107 }, client.WithAuthToken()) 108 if err != nil { 109 return util.CliError(err) 110 } 111 112 if v := rsp.Value.Data; len(v) == 0 || strings.TrimSpace(string(v)) == "null" { 113 return fmt.Errorf("not found") 114 } 115 116 if strings.HasPrefix(rsp.Value.Data, "\"") && strings.HasSuffix(rsp.Value.Data, "\"") { 117 fmt.Println(rsp.Value.Data[1 : len(rsp.Value.Data)-1]) 118 return nil 119 } 120 fmt.Println(string(rsp.Value.Data)) 121 return nil 122 } 123 124 func delConfig(ctx *cli.Context) error { 125 args := ctx.Args() 126 127 if args.Len() == 0 { 128 return cli.ShowSubcommandHelp(ctx) 129 } 130 // key val 131 key := args.Get(0) 132 if len(key) == 0 { 133 log.Fatal("key cannot be blank") 134 } 135 136 env, err := util.GetEnv(ctx) 137 if err != nil { 138 return err 139 } 140 ns, err := namespace.Get(env.Name) 141 if err != nil { 142 return err 143 } 144 145 // TODO: allow the specifying of a config.Key. This will be service name 146 // The actuall key-val set is a path e.g micro/accounts/key 147 pb := proto.NewConfigService("config", client.DefaultClient) 148 _, err = pb.Delete(context.DefaultContext, &proto.DeleteRequest{ 149 // The current namespace 150 Namespace: ns, 151 // The actual key for the val 152 Path: key, 153 }, client.WithAuthToken()) 154 return util.CliError(err) 155 } 156 157 func init() { 158 cmd.Register( 159 &cli.Command{ 160 Name: "config", 161 Usage: "Manage configuration values", 162 Action: helper.UnexpectedSubcommand, 163 Subcommands: []*cli.Command{ 164 { 165 Name: "get", 166 Usage: "Get a value; micro config get key", 167 Action: getConfig, 168 Flags: []cli.Flag{ 169 &cli.BoolFlag{ 170 Name: "secret", 171 Aliases: []string{"s"}, 172 Usage: "Set it as a secret value", 173 }, 174 }, 175 }, 176 { 177 Name: "set", 178 Usage: "Set a key-val; micro config set key val", 179 Action: setConfig, 180 Flags: []cli.Flag{ 181 &cli.BoolFlag{ 182 Name: "secret", 183 Aliases: []string{"s"}, 184 Usage: "Set it as a secret value", 185 }, 186 }, 187 }, 188 { 189 Name: "del", 190 Usage: "Delete a value; micro config del key", 191 Action: delConfig, 192 }, 193 }, 194 }, 195 ) 196 }