|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/AlekSi/pointer" |
| 9 | + "github.com/gitploy-io/gitploy/pkg/api" |
| 10 | + "github.com/tidwall/gjson" |
| 11 | + "github.com/urfave/cli/v2" |
| 12 | +) |
| 13 | + |
| 14 | +var repoUpdateCommand = &cli.Command{ |
| 15 | + Name: "update", |
| 16 | + Usage: "Update the repository.", |
| 17 | + ArgsUsage: "<owner>/<repo>", |
| 18 | + Flags: []cli.Flag{ |
| 19 | + &cli.StringFlag{ |
| 20 | + Name: "config", |
| 21 | + Aliases: []string{"C"}, |
| 22 | + Usage: "The path of configuration file.", |
| 23 | + }, |
| 24 | + &cli.StringFlag{ |
| 25 | + Name: "active", |
| 26 | + Aliases: []string{"A"}, |
| 27 | + Usage: "Activate or deactivate the repository. Ex 'true', 'false'", |
| 28 | + }, |
| 29 | + }, |
| 30 | + Action: func(cli *cli.Context) error { |
| 31 | + ns, n, err := splitFullName(cli.Args().First()) |
| 32 | + if err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + |
| 36 | + // Build the request body. |
| 37 | + req := api.RepoUpdateRequest{} |
| 38 | + if config := cli.String("config"); config != "" { |
| 39 | + req.ConfigPath = pointer.ToString(config) |
| 40 | + } |
| 41 | + |
| 42 | + if active := cli.String("active"); active != "" { |
| 43 | + b, err := strconv.ParseBool(active) |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("'%s' is invalid format: %w", active, err) |
| 46 | + } |
| 47 | + |
| 48 | + req.Active = pointer.ToBool(b) |
| 49 | + } |
| 50 | + |
| 51 | + c := buildClient(cli) |
| 52 | + repo, err := c.Repo.Update(cli.Context, ns, n, req) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + output, err := json.MarshalIndent(repo, "", " ") |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("Failed to marshal: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + if q := cli.String("query"); q != "" { |
| 63 | + fmt.Println(gjson.GetBytes(output, q)) |
| 64 | + return nil |
| 65 | + } |
| 66 | + |
| 67 | + fmt.Println(string(output)) |
| 68 | + return nil |
| 69 | + }, |
| 70 | +} |
0 commit comments