/
customizer_create_version.go
83 lines (68 loc) · 1.93 KB
/
customizer_create_version.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2023, SailPoint Technologies, Inc. All rights reserved.
package connector
import (
"archive/zip"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"github.com/olekukonko/tablewriter"
"github.com/sailpoint-oss/sailpoint-cli/internal/client"
"github.com/sailpoint-oss/sailpoint-cli/internal/util"
"github.com/spf13/cobra"
)
func newCustomizerCreateVersionCmd(client client.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "upload",
Short: "Upload connector customizer",
Example: "sail conn customizers upload -c 1234 -f path/to/zip/archive.zip",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
id := cmd.Flags().Lookup("id").Value.String()
archivePath := cmd.Flags().Lookup("file").Value.String()
f, err := os.Open(archivePath)
if err != nil {
return err
}
info, err := f.Stat()
if err != nil {
return err
}
_, err = zip.NewReader(f, info.Size())
if err != nil {
return err
}
_, err = f.Seek(0, io.SeekStart)
if err != nil {
return err
}
resp, err := client.Post(cmd.Context(), util.ResourceUrl(connectorCustomizersEndpoint, id, "versions"), "application/zip", f)
if err != nil {
return err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("upload customizer failed. status: %s\nbody: %s", resp.Status, string(body))
}
var cv customizerVersion
err = json.NewDecoder(resp.Body).Decode(&cv)
if err != nil {
return err
}
table := tablewriter.NewWriter(cmd.OutOrStdout())
table.SetHeader(customizerVersionColumns)
table.Append(cv.columns())
table.Render()
return nil
},
}
cmd.Flags().StringP("id", "c", "", "Connector customizer ID")
_ = cmd.MarkFlagRequired("id")
cmd.Flags().StringP("file", "f", "", "ZIP Archive")
_ = cmd.MarkFlagRequired("file")
return cmd
}