|
| 1 | +// Copyright © 2018 github.com/devopsctl authors |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal |
| 4 | +// in the Software without restriction, including without limitation the rights |
| 5 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 6 | +// copies of the Software, and to permit persons to whom the Software is |
| 7 | +// furnished to do so, subject to the following conditions: |
| 8 | +// |
| 9 | +// The above copyright notice and this permission notice shall be included in |
| 10 | +// all copies or substantial portions of the Software. |
| 11 | +// |
| 12 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 13 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 14 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 15 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 16 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 17 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 18 | +// THE SOFTWARE. |
| 19 | + |
| 20 | +package cmd |
| 21 | + |
| 22 | +import ( |
| 23 | + "github.com/spf13/cobra" |
| 24 | + gitlab "github.com/xanzy/go-gitlab" |
| 25 | +) |
| 26 | + |
| 27 | +var newTagCmd = &cobra.Command{ |
| 28 | + Use: "tag", |
| 29 | + Aliases: []string{"t"}, |
| 30 | + Short: "Create a new tag for a specified project", |
| 31 | + Example: `# create tag from master branch for project groupx/myapp |
| 32 | +gitlabctl new tag v2.0 --project=groupx/myapp --ref=master`, |
| 33 | + SilenceErrors: true, |
| 34 | + SilenceUsage: true, |
| 35 | + DisableAutoGenTag: true, |
| 36 | + Args: cobra.ExactArgs(1), |
| 37 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 38 | + return runNewTag(cmd, args[0]) |
| 39 | + }, |
| 40 | +} |
| 41 | + |
| 42 | +func init() { |
| 43 | + newCmd.AddCommand(newTagCmd) |
| 44 | + addProjectFlag(newTagCmd) |
| 45 | + verifyMarkFlagRequired(newTagCmd, "project") |
| 46 | + newTagCmd.Flags().StringP("ref", "r", "", |
| 47 | + "The branch name or commit SHA to create branch from") |
| 48 | + verifyMarkFlagRequired(newTagCmd, "ref") |
| 49 | +} |
| 50 | + |
| 51 | +func runNewTag(cmd *cobra.Command, tag string) error { |
| 52 | + opts := new(gitlab.CreateTagOptions) |
| 53 | + opts.Ref = gitlab.String(getFlagString(cmd, "ref")) |
| 54 | + opts.TagName = gitlab.String(tag) |
| 55 | + createdTag, err := newTag(getFlagString(cmd, "project"), opts) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + printTagsOut(getFlagString(cmd, "out"), createdTag) |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +func newTag(project string, opts *gitlab.CreateTagOptions ) (*gitlab.Tag, error) { |
| 64 | + git, err := newGitlabClient() |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + tag, _, err := git.Tags.CreateTag(project, opts) |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + return tag, nil |
| 73 | +} |
0 commit comments