|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + yaml "github.com/ghodss/yaml" |
| 11 | + "github.com/ovh/cds/cli" |
| 12 | + "github.com/ovh/cds/sdk/cdsclient" |
| 13 | +) |
| 14 | + |
| 15 | +var projectVCSCmd = cli.Command{ |
| 16 | + Name: "vcs", |
| 17 | + Aliases: []string{"vcs"}, |
| 18 | + Short: "Manage VCS on a CDS project", |
| 19 | +} |
| 20 | + |
| 21 | +func projectVCS() *cobra.Command { |
| 22 | + return cli.NewCommand(projectVCSCmd, nil, []*cobra.Command{ |
| 23 | + cli.NewListCommand(projectVCSListCmd, projectVCSListFunc, nil, withAllCommandModifiers()...), |
| 24 | + cli.NewDeleteCommand(projectVCSDeleteCmd, projectVCSDeleteFunc, nil, withAllCommandModifiers()...), |
| 25 | + cli.NewCommand(projectVCSImportCmd, projectVCSImportFunc, nil, withAllCommandModifiers()...), |
| 26 | + cli.NewCommand(projectVCSExportCmd, projectVCSExportFunc, nil, withAllCommandModifiers()...), |
| 27 | + }) |
| 28 | +} |
| 29 | + |
| 30 | +var projectVCSListCmd = cli.Command{ |
| 31 | + Name: "list", |
| 32 | + Short: "List VCS available on a project", |
| 33 | + Ctx: []cli.Arg{ |
| 34 | + {Name: _ProjectKey}, |
| 35 | + }, |
| 36 | +} |
| 37 | + |
| 38 | +func projectVCSListFunc(v cli.Values) (cli.ListResult, error) { |
| 39 | + pfs, err := client.ProjectVCSList(context.Background(), v.GetString(_ProjectKey)) |
| 40 | + return cli.AsListResult(pfs), err |
| 41 | +} |
| 42 | + |
| 43 | +var projectVCSDeleteCmd = cli.Command{ |
| 44 | + Name: "delete", |
| 45 | + Short: "Delete a VCS configuration on a project", |
| 46 | + Ctx: []cli.Arg{ |
| 47 | + {Name: _ProjectKey}, |
| 48 | + }, |
| 49 | + Args: []cli.Arg{ |
| 50 | + {Name: "name"}, |
| 51 | + }, |
| 52 | +} |
| 53 | + |
| 54 | +func projectVCSDeleteFunc(v cli.Values) error { |
| 55 | + return client.ProjectVCSDelete(context.Background(), v.GetString(_ProjectKey), v.GetString("name")) |
| 56 | +} |
| 57 | + |
| 58 | +var projectVCSImportCmd = cli.Command{ |
| 59 | + Name: "import", |
| 60 | + Short: "Import a VCS configuration on a project from a yaml file", |
| 61 | + Example: "cdsctl project vcs import MY-PROJECT file.yml", |
| 62 | + Ctx: []cli.Arg{ |
| 63 | + {Name: _ProjectKey}, |
| 64 | + }, |
| 65 | + Args: []cli.Arg{ |
| 66 | + {Name: "filename"}, |
| 67 | + }, |
| 68 | + Flags: []cli.Flag{ |
| 69 | + {Name: "force", Type: cli.FlagBool}, |
| 70 | + }, |
| 71 | +} |
| 72 | + |
| 73 | +func projectVCSImportFunc(v cli.Values) error { |
| 74 | + f, err := os.Open(v.GetString("filename")) |
| 75 | + if err != nil { |
| 76 | + return cli.WrapError(err, "unable to open file %s", v.GetString("filename")) |
| 77 | + } |
| 78 | + defer f.Close() |
| 79 | + |
| 80 | + var mods []cdsclient.RequestModifier |
| 81 | + if v.GetBool("force") { |
| 82 | + mods = append(mods, cdsclient.Force()) |
| 83 | + } |
| 84 | + |
| 85 | + _, err = client.ProjectVCSImport(context.Background(), v.GetString(_ProjectKey), f, mods...) |
| 86 | + return err |
| 87 | +} |
| 88 | + |
| 89 | +var projectVCSExportCmd = cli.Command{ |
| 90 | + Name: "export", |
| 91 | + Short: "Export a VCS configuration from a project to stdout", |
| 92 | + Example: "cdsctl vcs export MY-PROJECT MY-VCS-SERVER-NAME > file.yaml", |
| 93 | + Ctx: []cli.Arg{ |
| 94 | + {Name: _ProjectKey}, |
| 95 | + }, |
| 96 | + Args: []cli.Arg{ |
| 97 | + {Name: "name"}, |
| 98 | + }, |
| 99 | +} |
| 100 | + |
| 101 | +func projectVCSExportFunc(v cli.Values) error { |
| 102 | + pf, err := client.ProjectVCSGet(context.Background(), v.GetString(_ProjectKey), v.GetString("name")) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + btes, err := yaml.Marshal(pf) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + fmt.Println(string(btes)) |
| 113 | + return nil |
| 114 | +} |
0 commit comments