|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/joho/godotenv" |
| 9 | + "github.com/urfave/cli/v3" |
| 10 | + "go.wasmcloud.dev/wasmbus/control" |
| 11 | +) |
| 12 | + |
| 13 | +func configCommand() *cli.Command { |
| 14 | + var targetName string |
| 15 | + var nameArg = &cli.StringArg{ |
| 16 | + Name: "name", |
| 17 | + Destination: &targetName, |
| 18 | + Max: 1, |
| 19 | + } |
| 20 | + return &cli.Command{ |
| 21 | + Name: "config", |
| 22 | + Usage: "Interact with Lattice Config", |
| 23 | + Flags: []cli.Flag{}, |
| 24 | + Commands: []*cli.Command{ |
| 25 | + { |
| 26 | + Name: "get", |
| 27 | + Usage: "Get a config", |
| 28 | + Action: wrapNamedAction(getConfigCommand, &targetName), |
| 29 | + Arguments: []cli.Argument{ |
| 30 | + nameArg, |
| 31 | + }, |
| 32 | + }, |
| 33 | + { |
| 34 | + Name: "delete", |
| 35 | + Usage: "Delete a config", |
| 36 | + Action: wrapNamedAction(deleteConfigCommand, &targetName), |
| 37 | + Arguments: []cli.Argument{ |
| 38 | + nameArg, |
| 39 | + }, |
| 40 | + }, |
| 41 | + { |
| 42 | + Name: "put", |
| 43 | + Usage: "Put a config", |
| 44 | + Action: putConfigCommand, |
| 45 | + Flags: []cli.Flag{ |
| 46 | + &cli.StringFlag{ |
| 47 | + Name: "name", |
| 48 | + Usage: "Name of the config to store", |
| 49 | + Required: true, |
| 50 | + }, |
| 51 | + &cli.StringFlag{ |
| 52 | + Name: "file", |
| 53 | + Aliases: []string{"f"}, |
| 54 | + Usage: "File to read config from, in dotenv format (one KEY=VALUE per line)", |
| 55 | + Required: true, |
| 56 | + }, |
| 57 | + }, |
| 58 | + Arguments: []cli.Argument{ |
| 59 | + nameArg, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func deleteConfigCommand(ctx context.Context, cmd *cli.Command, name string) error { |
| 67 | + client, err := controlClientFromCommand(cmd) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + resp, err := client.ConfigDelete(ctx, &control.ConfigDeleteRequest{ |
| 73 | + Name: name, |
| 74 | + }) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + if !resp.Success { |
| 79 | + return fmt.Errorf("received error response: %s", resp.Message) |
| 80 | + } |
| 81 | + |
| 82 | + fmt.Println(titleStyle.Render("⁜", name), "deleted") |
| 83 | + |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +func getConfigCommand(ctx context.Context, cmd *cli.Command, name string) error { |
| 88 | + client, err := controlClientFromCommand(cmd) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + resp, err := client.ConfigGet(ctx, &control.ConfigGetRequest{ |
| 94 | + Name: name, |
| 95 | + }) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + if !resp.Success { |
| 100 | + return fmt.Errorf("received error response: %s", resp.Message) |
| 101 | + } |
| 102 | + |
| 103 | + // NOTE(lxf): This feels like a bug in hosts' crates/host/src/wasmbus/mod.rs |
| 104 | + if resp.Message == "Configuration not found" { |
| 105 | + return fmt.Errorf("configuration not found") |
| 106 | + } |
| 107 | + |
| 108 | + config, err := godotenv.Marshal(resp.Response) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + fmt.Println(config) |
| 113 | + |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +func putConfigCommand(ctx context.Context, cmd *cli.Command) error { |
| 118 | + name := cmd.String("name") |
| 119 | + client, err := controlClientFromCommand(cmd) |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + |
| 124 | + f, err := os.ReadFile(cmd.String("file")) |
| 125 | + if err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + |
| 129 | + var values map[string]string |
| 130 | + if values, err = godotenv.UnmarshalBytes(f); err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + |
| 134 | + resp, err := client.ConfigPut(ctx, &control.ConfigPutRequest{ |
| 135 | + Name: name, |
| 136 | + Values: values, |
| 137 | + }) |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + if !resp.Success { |
| 142 | + return fmt.Errorf("received error response: %s", resp.Message) |
| 143 | + } |
| 144 | + |
| 145 | + fmt.Println(titleStyle.Render("⁜", name), "stored") |
| 146 | + |
| 147 | + return nil |
| 148 | +} |
0 commit comments