-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cdsctl,sdk): manage vcs on projects (#6132)
* feat(cdsctl,sdk): manage vcs on projects Signed-off-by: Yvonnick Esnault <[email protected]>
- Loading branch information
Showing
26 changed files
with
399 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/ovh/cds/cli" | ||
) | ||
|
||
var experimentalCmd = cli.Command{ | ||
Name: "experimental", | ||
Aliases: []string{"exp"}, | ||
Short: "CDS Experimental commands", | ||
Hidden: true, | ||
} | ||
|
||
func experimentalCommands() []*cobra.Command { | ||
return []*cobra.Command{ | ||
experimentalProject(), | ||
} | ||
} | ||
|
||
func experimental() *cobra.Command { | ||
return cli.NewCommand(experimentalCmd, nil, experimentalCommands()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/ovh/cds/cli" | ||
) | ||
|
||
var experimentalProjectCmd = cli.Command{ | ||
Name: "project", | ||
Short: "CDS Experimental project commands", | ||
} | ||
|
||
func experimentalProject() *cobra.Command { | ||
return cli.NewCommand(experimentalProjectCmd, nil, []*cobra.Command{ | ||
projectVCS(), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
yaml "github.com/ghodss/yaml" | ||
"github.com/ovh/cds/cli" | ||
"github.com/ovh/cds/sdk/cdsclient" | ||
) | ||
|
||
var projectVCSCmd = cli.Command{ | ||
Name: "vcs", | ||
Aliases: []string{"vcs"}, | ||
Short: "Manage VCS on a CDS project", | ||
} | ||
|
||
func projectVCS() *cobra.Command { | ||
return cli.NewCommand(projectVCSCmd, nil, []*cobra.Command{ | ||
cli.NewListCommand(projectVCSListCmd, projectVCSListFunc, nil, withAllCommandModifiers()...), | ||
cli.NewDeleteCommand(projectVCSDeleteCmd, projectVCSDeleteFunc, nil, withAllCommandModifiers()...), | ||
cli.NewCommand(projectVCSImportCmd, projectVCSImportFunc, nil, withAllCommandModifiers()...), | ||
cli.NewCommand(projectVCSExportCmd, projectVCSExportFunc, nil, withAllCommandModifiers()...), | ||
}) | ||
} | ||
|
||
var projectVCSListCmd = cli.Command{ | ||
Name: "list", | ||
Short: "List VCS available on a project", | ||
Ctx: []cli.Arg{ | ||
{Name: _ProjectKey}, | ||
}, | ||
} | ||
|
||
func projectVCSListFunc(v cli.Values) (cli.ListResult, error) { | ||
pfs, err := client.ProjectVCSList(context.Background(), v.GetString(_ProjectKey)) | ||
return cli.AsListResult(pfs), err | ||
} | ||
|
||
var projectVCSDeleteCmd = cli.Command{ | ||
Name: "delete", | ||
Short: "Delete a VCS configuration on a project", | ||
Ctx: []cli.Arg{ | ||
{Name: _ProjectKey}, | ||
}, | ||
Args: []cli.Arg{ | ||
{Name: "name"}, | ||
}, | ||
} | ||
|
||
func projectVCSDeleteFunc(v cli.Values) error { | ||
return client.ProjectVCSDelete(context.Background(), v.GetString(_ProjectKey), v.GetString("name")) | ||
} | ||
|
||
var projectVCSImportCmd = cli.Command{ | ||
Name: "import", | ||
Short: "Import a VCS configuration on a project from a yaml file", | ||
Example: "cdsctl project vcs import MY-PROJECT file.yml", | ||
Ctx: []cli.Arg{ | ||
{Name: _ProjectKey}, | ||
}, | ||
Args: []cli.Arg{ | ||
{Name: "filename"}, | ||
}, | ||
Flags: []cli.Flag{ | ||
{Name: "force", Type: cli.FlagBool}, | ||
}, | ||
} | ||
|
||
func projectVCSImportFunc(v cli.Values) error { | ||
f, err := os.Open(v.GetString("filename")) | ||
if err != nil { | ||
return cli.WrapError(err, "unable to open file %s", v.GetString("filename")) | ||
} | ||
defer f.Close() | ||
|
||
var mods []cdsclient.RequestModifier | ||
if v.GetBool("force") { | ||
mods = append(mods, cdsclient.Force()) | ||
} | ||
|
||
_, err = client.ProjectVCSImport(context.Background(), v.GetString(_ProjectKey), f, mods...) | ||
return err | ||
} | ||
|
||
var projectVCSExportCmd = cli.Command{ | ||
Name: "export", | ||
Short: "Export a VCS configuration from a project to stdout", | ||
Example: "cdsctl vcs export MY-PROJECT MY-VCS-SERVER-NAME > file.yaml", | ||
Ctx: []cli.Arg{ | ||
{Name: _ProjectKey}, | ||
}, | ||
Args: []cli.Arg{ | ||
{Name: "name"}, | ||
}, | ||
} | ||
|
||
func projectVCSExportFunc(v cli.Values) error { | ||
pf, err := client.ProjectVCSGet(context.Background(), v.GetString(_ProjectKey), v.GetString("name")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
btes, err := yaml.Marshal(pf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(string(btes)) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ func main() { | |
encrypt(), | ||
contexts(), | ||
environment(), | ||
experimental(), | ||
events(), | ||
group(), | ||
health(), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package cdsclient | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/ghodss/yaml" | ||
|
||
"github.com/ovh/cds/sdk" | ||
) | ||
|
||
func (c *client) ProjectVCSGet(ctx context.Context, projectKey string, vcsName string) (sdk.VCSProject, error) { | ||
path := fmt.Sprintf("/v2/project/%s/vcs/%s", projectKey, vcsName) | ||
var pf sdk.VCSProject | ||
if _, err := c.GetJSON(ctx, path, &pf); err != nil { | ||
return pf, err | ||
} | ||
return pf, nil | ||
} | ||
|
||
func (c *client) ProjectVCSList(ctx context.Context, projectKey string) ([]sdk.VCSProject, error) { | ||
path := fmt.Sprintf("/v2/project/%s/vcs", projectKey) | ||
var pfs []sdk.VCSProject | ||
if _, err := c.GetJSON(ctx, path, &pfs); err != nil { | ||
return pfs, err | ||
} | ||
return pfs, nil | ||
} | ||
|
||
func (c *client) ProjectVCSDelete(ctx context.Context, projectKey string, vcsName string) error { | ||
path := fmt.Sprintf("/v2/project/%s/vcs/%s", projectKey, vcsName) | ||
var pf sdk.VCSProject | ||
if _, err := c.DeleteJSON(ctx, path, &pf); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (c *client) ProjectVCSImport(ctx context.Context, projectKey string, content io.Reader, mods ...RequestModifier) (sdk.VCSProject, error) { | ||
var pf sdk.VCSProject | ||
|
||
body, err := io.ReadAll(content) | ||
if err != nil { | ||
return pf, err | ||
} | ||
|
||
if err := yaml.Unmarshal(body, &pf); err != nil { | ||
return pf, err | ||
} | ||
|
||
oldvcs, _ := c.ProjectVCSGet(ctx, projectKey, pf.Name) | ||
if oldvcs.Name == "" { | ||
path := fmt.Sprintf("/v2/project/%s/vcs", projectKey) | ||
if _, err := c.PostJSON(ctx, path, &pf, &pf, mods...); err != nil { | ||
return pf, err | ||
} | ||
return pf, nil | ||
} | ||
|
||
path := fmt.Sprintf("/v2/project/%s/vcs/%s", projectKey, pf.Name) | ||
if _, err := c.PutJSON(ctx, path, &pf, &pf, mods...); err != nil { | ||
return pf, err | ||
} | ||
return pf, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.