mirror of
https://github.com/LukeHagar/sailpoint-cli.git
synced 2025-12-09 20:57:44 +00:00
76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
// Copyright (c) 2021, SailPoint Technologies, Inc. All rights reserved.
|
|
package transform
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/fatih/color"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func newDownloadCmd() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "download",
|
|
Short: "download transforms",
|
|
Long: "Download transforms to local storage",
|
|
Example: "sail transform dl -d transform_files|\nsail transform dl",
|
|
Aliases: []string{"dl"},
|
|
Args: cobra.NoArgs,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
transforms, err := GetTransforms()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
destination := cmd.Flags().Lookup("destination").Value.String()
|
|
|
|
err = ListTransforms()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, v := range transforms {
|
|
filename := strings.ReplaceAll(v.Name, " ", "") + ".json"
|
|
content, _ := json.MarshalIndent(v, "", " ")
|
|
|
|
var err error
|
|
if destination == "" {
|
|
destination = "transform_files"
|
|
}
|
|
|
|
// Make sure the output dir exists first
|
|
err = os.MkdirAll(destination, os.ModePerm)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Make sure to create the files if they dont exist
|
|
file, err := os.OpenFile((filepath.Join(destination, filename)), os.O_RDWR|os.O_CREATE, 0777)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = file.Write(content)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
color.Green("Transforms downloaded successfully to %v", destination)
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Flags().StringP("destination", "d", "", "The path to the directory to save the files in (default current working directory). If the directory doesn't exist, then it will be automatically created.")
|
|
|
|
return cmd
|
|
}
|