diff --git a/cmd/environments.go b/cmd/environments.go new file mode 100644 index 0000000..95d0973 --- /dev/null +++ b/cmd/environments.go @@ -0,0 +1,182 @@ +// Copyright © 2018 Michael Lihs +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +package cmd + +import ( + "errors" + + . "github.com/michaellihs/golab/cmd/helpers" + + "github.com/spf13/cobra" + "github.com/xanzy/go-gitlab" +) + +// see https://docs.gitlab.com/ce/api/environments.html +var environmentsCmd = &golabCommand{ + Parent: RootCmd, + Cmd: &cobra.Command{ + Use: "environments", + Aliases: []string{"environment", "env"}, + Short: "Manage environments", + Long: `Manage environments`, + }, + Run: func(cmd golabCommand) error { + return errors.New("you cannot call this command without any subcommand") + }, +} + +// see https://docs.gitlab.com/ce/api/environments.html#list-environments +type environmentsListFlag struct { + Id *string `flag_name:"id" short:"i" type:"string" required:"yes" description:"The ID or URL-encoded path of the project owned by the authenticated user"` +} + +var environmentsListCmd = &golabCommand{ + Parent: environmentsCmd.Cmd, + Flags: &environmentsListFlag{}, + Opts: &gitlab.ListEnvironmentsOptions{}, + Cmd: &cobra.Command{ + Use: "list", + Aliases: []string{"ls"}, + Short: "List environments", + Long: `Get all environments for a project`, + }, + Run: func(cmd golabCommand) error { + flags := cmd.Flags.(*environmentsListFlag) + opts := cmd.Opts.(*gitlab.ListEnvironmentsOptions) + environments, _, err := gitlabClient.Environments.ListEnvironments(*flags.Id, opts) + if err != nil { + return err + } + return OutputJson(environments) + }, +} + +// see https://docs.gitlab.com/ce/api/environments.html#create-a-new-environment +type environmentsCreateFlags struct { + Id *string `flag_name:"id" short:"i" type:"integer/string" required:"yes" description:"The ID or URL-encoded path of the project owned by the authenticated user"` + Name *string `flag_name:"name" type:"string" required:"yes" description:"The name of the environment"` + ExternalURL *string `flag_name:"external_url" type:"string" required:"no" description:"Place to link to for this environment"` +} + +var environmentsCreateCmd = &golabCommand{ + Parent: environmentsCmd.Cmd, + Flags: &environmentsCreateFlags{}, + Opts: &gitlab.CreateEnvironmentOptions{}, + Cmd: &cobra.Command{ + Use: "create", + Short: "Create a new environment", + Long: `Creates a new environment for the given project with the given name and external URL.`, + }, + Run: func(cmd golabCommand) error { + flags := cmd.Flags.(*environmentsCreateFlags) + opts := cmd.Opts.(*gitlab.CreateEnvironmentOptions) + environment, _, err := gitlabClient.Environments.CreateEnvironment(*flags.Id, opts) + if err != nil { + return err + } + return OutputJson(environment) + }, +} + +// see https://docs.gitlab.com/ce/api/environments.html#delete-an-environment +type environmentsDeleteFlags struct { + Id *string `flag_name:"id" short:"i" type:"string" required:"yes" description:"The ID or URL-encoded path of the project owned by the authenticated user"` + EnvironmentId *int `flag_name:"environment_id" short:"e" type:"integer" required:"yes" description:"The ID of the environment"` +} + +var environmentsDeleteCmd = &golabCommand{ + Parent: environmentsCmd.Cmd, + Flags: &environmentsDeleteFlags{}, + Cmd: &cobra.Command{ + Use: "delete", + Aliases: []string{"rm"}, + Short: "Delete an environment", + Long: `Deletes an environment with a given ID.`, + }, + Run: func(cmd golabCommand) error { + flags := cmd.Flags.(*environmentsDeleteFlags) + _, err := gitlabClient.Environments.DeleteEnvironment(*flags.Id, *flags.EnvironmentId) + return err + }, +} + +// see https://docs.gitlab.com/ce/api/environments.html#edit-an-existing-environment +type environmentsEditFlags struct { + Id *string `flag_name:"id" short:"i" type:"integer/string" required:"yes" description:"The ID or URL-encoded path of the project owned by the authenticated user"` + EnvironmentId *int `flag_name:"environment_id" short:"e" type:"integer" required:"yes" description:"The ID of the environment"` + Name *string `flag_name:"name" type:"string" required:"no" description:"The new name of the environment"` + ExternalURL *string `flag_name:"external_url" type:"string" required:"yes" description:"The new external_url"` +} + +var environmentsEditCmd = &golabCommand{ + Parent: environmentsCmd.Cmd, + Flags: &environmentsEditFlags{}, + Opts: &gitlab.EditEnvironmentOptions{}, + Cmd: &cobra.Command{ + Use: "edit", + Aliases: []string{"update"}, + Short: "Edit an existing environment", + Long: `Updates an existing environment's name and/or external_url. + +It returns 200 if the environment was successfully updated. In case of an error, a status code 400 is returned.`, + }, + Run: func(cmd golabCommand) error { + flags := cmd.Flags.(*environmentsEditFlags) + opts := cmd.Opts.(*gitlab.EditEnvironmentOptions) + environment, _, err := gitlabClient.Environments.EditEnvironment(*flags.Id, *flags.EnvironmentId, opts) + if err != nil { + return err + } + return OutputJson(environment) + }, +} + +// see https://docs.gitlab.com/ce/api/environments.html#stop-an-environment +/* TODO not implemented in go-gitlab yet +type environmentsStopFlags struct { + Id *string `flag_name:"id" short:"i" type:"integer/string" required:"yes" description:"The ID or URL-encoded path of the project owned by the authenticated user"` + EnvironmentId *int `flag_name:"environment_id" short:"e" type:"integer" required:"yes" description:"The ID of the environment"` +} + +var environmentsStopCmd = &golabCommand{ + Parent: environmentsCmd.Cmd, + Flags: &environmentsStopFlags{}, + Cmd: &cobra.Command{ + Use: "stop", + Aliases: []string{""}, + Short: "Stop an environment", + Long: `It returns 200 if the environment was successfully stopped, and 404 if the environment does not exist.`, + }, + Run: func(cmd golabCommand) error { + flags := cmd.Flags.(*environmentsStopFlags) + return gitlabClient.Environments.StopEnvironemt(*flags.Id, *flags.EnvironmentId) + }, +} +*/ + +func init() { + environmentsCmd.Init() + environmentsListCmd.Init() + environmentsCreateCmd.Init() + environmentsEditCmd.Init() + environmentsDeleteCmd.Init() + //environmentsStopCmd.Init() +} diff --git a/doc/golab.md b/doc/golab.md index a1e640a..6b8b670 100644 --- a/doc/golab.md +++ b/doc/golab.md @@ -20,6 +20,7 @@ This application provides a Command Line Interface for Gitlab. * [golab branches](golab_branches.md) - Branches * [golab commits](golab_commits.md) - Manage Commits * [golab deploy-keys](golab_deploy-keys.md) - Deploy Keys API +* [golab environments](golab_environments.md) - Manage environments * [golab gendoc](golab_gendoc.md) - Render the Markdown Documentation for golab * [golab group](golab_group.md) - Manage Gitlab Groups * [golab group-members](golab_group-members.md) - Access group members diff --git a/doc/golab_environments.md b/doc/golab_environments.md new file mode 100644 index 0000000..d71c751 --- /dev/null +++ b/doc/golab_environments.md @@ -0,0 +1,34 @@ +## golab environments + +Manage environments + +### Synopsis + + +Manage environments + +``` +golab environments [flags] +``` + +### Options + +``` + -h, --help help for environments +``` + +### Options inherited from parent commands + +``` + --ca-file string (optional) provides a .pem file to be used in certificates pool for SSL connection + --ca-path string (optional) provides a directory with .pem certificates to be used for SSL connection + --config string (optional) golab config file (default is ./.golab.yml and $HOME/.golab.yml) +``` + +### SEE ALSO +* [golab](golab.md) - Gitlab CLI written in Go +* [golab environments create](golab_environments_create.md) - Create a new environment +* [golab environments delete](golab_environments_delete.md) - Delete an environment +* [golab environments edit](golab_environments_edit.md) - Edit an existing environment +* [golab environments list](golab_environments_list.md) - List environments + diff --git a/doc/golab_environments_create.md b/doc/golab_environments_create.md new file mode 100644 index 0000000..6b04f46 --- /dev/null +++ b/doc/golab_environments_create.md @@ -0,0 +1,33 @@ +## golab environments create + +Create a new environment + +### Synopsis + + +Creates a new environment for the given project with the given name and external URL. + +``` +golab environments create [flags] +``` + +### Options + +``` + --external_url string (optional) Place to link to for this environment + -h, --help help for create + -i, --id string (required) The ID or URL-encoded path of the project owned by the authenticated user + --name string (required) The name of the environment +``` + +### Options inherited from parent commands + +``` + --ca-file string (optional) provides a .pem file to be used in certificates pool for SSL connection + --ca-path string (optional) provides a directory with .pem certificates to be used for SSL connection + --config string (optional) golab config file (default is ./.golab.yml and $HOME/.golab.yml) +``` + +### SEE ALSO +* [golab environments](golab_environments.md) - Manage environments + diff --git a/doc/golab_environments_delete.md b/doc/golab_environments_delete.md new file mode 100644 index 0000000..aed5e76 --- /dev/null +++ b/doc/golab_environments_delete.md @@ -0,0 +1,32 @@ +## golab environments delete + +Delete an environment + +### Synopsis + + +Deletes an environment with a given ID. + +``` +golab environments delete [flags] +``` + +### Options + +``` + -e, --environment_id int (required) The ID of the environment + -h, --help help for delete + -i, --id string (required) The ID or URL-encoded path of the project owned by the authenticated user +``` + +### Options inherited from parent commands + +``` + --ca-file string (optional) provides a .pem file to be used in certificates pool for SSL connection + --ca-path string (optional) provides a directory with .pem certificates to be used for SSL connection + --config string (optional) golab config file (default is ./.golab.yml and $HOME/.golab.yml) +``` + +### SEE ALSO +* [golab environments](golab_environments.md) - Manage environments + diff --git a/doc/golab_environments_edit.md b/doc/golab_environments_edit.md new file mode 100644 index 0000000..aaa139e --- /dev/null +++ b/doc/golab_environments_edit.md @@ -0,0 +1,36 @@ +## golab environments edit + +Edit an existing environment + +### Synopsis + + +Updates an existing environment's name and/or external_url. + +It returns 200 if the environment was successfully updated. In case of an error, a status code 400 is returned. + +``` +golab environments edit [flags] +``` + +### Options + +``` + -e, --environment_id int (required) The ID of the environment + --external_url string (required) The new external_url + -h, --help help for edit + -i, --id string (required) The ID or URL-encoded path of the project owned by the authenticated user + --name string (optional) The new name of the environment +``` + +### Options inherited from parent commands + +``` + --ca-file string (optional) provides a .pem file to be used in certificates pool for SSL connection + --ca-path string (optional) provides a directory with .pem certificates to be used for SSL connection + --config string (optional) golab config file (default is ./.golab.yml and $HOME/.golab.yml) +``` + +### SEE ALSO +* [golab environments](golab_environments.md) - Manage environments + diff --git a/doc/golab_environments_list.md b/doc/golab_environments_list.md new file mode 100644 index 0000000..f4d7e3d --- /dev/null +++ b/doc/golab_environments_list.md @@ -0,0 +1,31 @@ +## golab environments list + +List environments + +### Synopsis + + +Get all environments for a project + +``` +golab environments list [flags] +``` + +### Options + +``` + -h, --help help for list + -i, --id string (required) The ID or URL-encoded path of the project owned by the authenticated user +``` + +### Options inherited from parent commands + +``` + --ca-file string (optional) provides a .pem file to be used in certificates pool for SSL connection + --ca-path string (optional) provides a directory with .pem certificates to be used for SSL connection + --config string (optional) golab config file (default is ./.golab.yml and $HOME/.golab.yml) +``` + +### SEE ALSO +* [golab environments](golab_environments.md) - Manage environments + diff --git a/zsh/_golab b/zsh/_golab index b057509..a6a08c0 100644 --- a/zsh/_golab +++ b/zsh/_golab @@ -9,7 +9,7 @@ case $state in level1) case $words[1] in golab) - _arguments '1: :(branches commits deploy-keys gendoc group group-members help labels login merge-requests namespaces open personal-access-token project protected-branches user version zsh-completion)' + _arguments '1: :(branches commits deploy-keys environments gendoc group group-members help labels login merge-requests namespaces open personal-access-token project protected-branches user version zsh-completion)' ;; *) _arguments '*: :_files' @@ -18,39 +18,42 @@ case $state in ;; level2) case $words[2] in - deploy-keys) - _arguments '2: :(add delete enable get list list-all)' - ;; - group) - _arguments '2: :(create delete get ls projects search transfer-project update)' + group-members) + _arguments '2: :(add delete edit get ls sync)' ;; labels) _arguments '2: :(create delete edit list subscribe unsubscribe)' ;; + protected-branches) + _arguments '2: :(get ls protect-branch unprotect-branch)' + ;; + group) + _arguments '2: :(create delete get ls projects search transfer-project update)' + ;; merge-requests) _arguments '2: :(accept add-spent-time cancel-when-pipeline-succeeds create create-todo delete get get-changes get-commits get-diff-version get-diff-versions list-issues ls project-ls reset-spent-time reset-time-estimate set-time-estimate subscribe time-tracking-stats unsubscribe update)' ;; + namespaces) + _arguments '2: :(get ls search)' + ;; project) _arguments '2: :(archive create delete edit fork forks get hooks housekeeping list-forks ls search share star unarchive unshare unstar upload-file)' ;; - protected-branches) - _arguments '2: :(get ls protect-branch unprotect-branch)' - ;; - user) - _arguments '2: :(activities block create delete emails get get-as-admin impersonation-token ls modify ssh-keys unblock)' - ;; branches) _arguments '2: :(create delete delete-merged get list protect unprotect)' ;; - group-members) - _arguments '2: :(add delete edit get ls sync)' - ;; - namespaces) - _arguments '2: :(get ls search)' - ;; commits) _arguments '2: :(create list)' ;; + deploy-keys) + _arguments '2: :(add delete enable get list list-all)' + ;; + environments) + _arguments '2: :(create delete edit list)' + ;; + user) + _arguments '2: :(activities block create delete emails get get-as-admin impersonation-token ls modify ssh-keys unblock)' + ;; *) _arguments '*: :_files' ;; @@ -58,12 +61,6 @@ case $state in ;; level3) case $words[3] in - hooks) - _arguments '3: :(add delete edit get ls)' - ;; - emails) - _arguments '3: :(add delete get ls)' - ;; impersonation-token) _arguments '3: :(create get get-all revoke)' ;; @@ -73,6 +70,12 @@ case $state in forks) _arguments '3: :(create delete)' ;; + hooks) + _arguments '3: :(add delete edit get ls)' + ;; + emails) + _arguments '3: :(add delete get ls)' + ;; *) _arguments '*: :_files' ;;