-
Notifications
You must be signed in to change notification settings - Fork 332
/
tag_create.go
68 lines (57 loc) · 2.15 KB
/
tag_create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package cmd
import (
"fmt"
"net/http"
"github.com/spf13/cobra"
"github.com/treeverse/lakefs/pkg/api/apigen"
)
const tagCreateRequiredArgs = 2
var tagCreateCmd = &cobra.Command{
Use: "create <tag URI> <commit URI>",
Short: "Create a new tag in a repository",
Example: "lakectl tag create lakefs://example-repo/example-tag lakefs://example-repo/2397cc9a9d04c20a4e5739b42c1dd3d8ba655c0b3a3b974850895a13d8bf9917",
Args: cobra.ExactArgs(tagCreateRequiredArgs),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) >= tagCreateRequiredArgs {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return validRepositoryToComplete(cmd.Context(), toComplete)
},
Run: func(cmd *cobra.Command, args []string) {
tagURI := MustParseRefURI("tag URI", args[0])
commitURI := MustParseRefURI("commit URI", args[1])
client := getClient()
ctx := cmd.Context()
force := Must(cmd.Flags().GetBool("force"))
if tagURI.Repository != commitURI.Repository {
Die("both references must belong to the same repository", 1)
}
if force {
// checking the validity of the commitRef before deleting the old one
res, err := client.GetCommitWithResponse(ctx, tagURI.Repository, commitURI.Ref)
DieOnErrorOrUnexpectedStatusCode(res, err, http.StatusOK)
if res.JSON200 == nil {
Die("Bad response from server", 1)
}
resp, err := client.DeleteTagWithResponse(ctx, tagURI.Repository, tagURI.Ref, &apigen.DeleteTagParams{})
if err != nil && (resp == nil || resp.JSON404 == nil) {
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusNoContent)
}
}
resp, err := client.CreateTagWithResponse(ctx, tagURI.Repository, apigen.CreateTagJSONRequestBody{
Id: tagURI.Ref,
Ref: commitURI.Ref,
})
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusCreated)
if resp.JSON201 == nil {
Die("Bad response from server", 1)
}
commitID := *resp.JSON201
fmt.Printf("Created tag '%s' (%s)\n", tagURI.Ref, commitID)
},
}
//nolint:gochecknoinits
func init() {
tagCreateCmd.Flags().BoolP("force", "f", false, "override the tag if it exists")
tagCmd.AddCommand(tagCreateCmd)
}