feat: added cacerts and some tidying
This commit is contained in:
parent
2112e7eefd
commit
bdd85b01ff
8 changed files with 279 additions and 59 deletions
176
cmd/upload.go
176
cmd/upload.go
|
|
@ -4,6 +4,8 @@ import (
|
|||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"maps"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
|
@ -41,12 +43,14 @@ var uploadCmd = &cobra.Command{
|
|||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
setenv(cmd, "host", "MAKESHIFT_HOST")
|
||||
setenv(cmd, "path", "MAKESHIFT_PATH")
|
||||
setenv(cmd, "cacert", "MAKESHIFT_CACERT")
|
||||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
var (
|
||||
host, _ = cmd.Flags().GetString("host")
|
||||
path, _ = cmd.Flags().GetString("path")
|
||||
dataArgs, _ = cmd.Flags().GetStringArray("data")
|
||||
host, _ = cmd.Flags().GetString("host")
|
||||
path, _ = cmd.Flags().GetString("path")
|
||||
cacertPath, _ = cmd.Flags().GetString("cacert")
|
||||
dataArgs, _ = cmd.Flags().GetStringArray("data")
|
||||
|
||||
inputData = processFiles(dataArgs)
|
||||
useDirectoryPath = len(inputData) > 1
|
||||
|
|
@ -55,8 +59,21 @@ var uploadCmd = &cobra.Command{
|
|||
query string
|
||||
err error
|
||||
)
|
||||
|
||||
log.Debug().
|
||||
Str("host", host).
|
||||
Str("path", path).
|
||||
Str("query", query).
|
||||
Str("cacert", cacertPath).
|
||||
Any("input", inputData).
|
||||
Send()
|
||||
|
||||
if cacertPath != "" {
|
||||
c.LoadCertificateFromPath(cacertPath)
|
||||
}
|
||||
|
||||
for inputPath, contents := range inputData {
|
||||
log.Info().Str("path", path).Int("size", len(contents)).Send()
|
||||
log.Debug().Str("path", path).Int("size", len(contents)).Send()
|
||||
if useDirectoryPath {
|
||||
query = path + "/" + filepath.Clean(inputPath)
|
||||
} else {
|
||||
|
|
@ -92,9 +109,10 @@ var uploadProfilesCmd = &cobra.Command{
|
|||
Short: "Upload a new profile",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
var (
|
||||
host, _ = cmd.Flags().GetString("host")
|
||||
dataArgs, _ = cmd.Flags().GetStringArray("data")
|
||||
profiles = processProfiles(dataArgs)
|
||||
host, _ = cmd.Flags().GetString("host")
|
||||
dataArgs, _ = cmd.Flags().GetStringArray("data")
|
||||
cacertPath, _ = cmd.Flags().GetString("cacert")
|
||||
profiles = processProfiles(dataArgs)
|
||||
|
||||
c = client.New(host)
|
||||
res *http.Response
|
||||
|
|
@ -103,6 +121,16 @@ var uploadProfilesCmd = &cobra.Command{
|
|||
err error
|
||||
)
|
||||
|
||||
log.Debug().
|
||||
Str("host", host).
|
||||
Str("query", query).
|
||||
Str("cacert", cacertPath).
|
||||
Send()
|
||||
|
||||
if cacertPath != "" {
|
||||
c.LoadCertificateFromPath(cacertPath)
|
||||
}
|
||||
|
||||
// load files from args
|
||||
for i, path := range args {
|
||||
body, err = os.ReadFile(path)
|
||||
|
|
@ -160,8 +188,9 @@ var uploadPluginsCmd = &cobra.Command{
|
|||
// make one request be host positional argument (restricted to 1 for now)
|
||||
// temp := append(handleArgs(args), processDataArgs(dataArgs)...)
|
||||
var (
|
||||
host, _ = cmd.Flags().GetString("host")
|
||||
dataArgs, _ = cmd.Flags().GetStringArray("data")
|
||||
host, _ = cmd.Flags().GetString("host")
|
||||
dataArgs, _ = cmd.Flags().GetStringArray("data")
|
||||
cacertPath, _ = cmd.Flags().GetString("cacert")
|
||||
|
||||
plugins = processFiles(dataArgs)
|
||||
c = client.New(host)
|
||||
|
|
@ -172,6 +201,16 @@ var uploadPluginsCmd = &cobra.Command{
|
|||
err error
|
||||
)
|
||||
|
||||
log.Debug().
|
||||
Str("host", host).
|
||||
Str("query", query).
|
||||
Str("cacert", cacertPath).
|
||||
Send()
|
||||
|
||||
if cacertPath != "" {
|
||||
c.LoadCertificateFromPath(cacertPath)
|
||||
}
|
||||
|
||||
// load files from args
|
||||
for i, path := range args {
|
||||
body, err = os.ReadFile(path)
|
||||
|
|
@ -208,6 +247,8 @@ var uploadPluginsCmd = &cobra.Command{
|
|||
func init() {
|
||||
uploadCmd.PersistentFlags().String("host", "http://localhost:5050", "Set the makeshift remote host (can be set with MAKESHIFT_HOST)")
|
||||
uploadCmd.PersistentFlags().StringArrayP("data", "d", []string{}, "Set the data to send to specified host (prepend @ for files)")
|
||||
uploadCmd.PersistentFlags().String("cacert", "", "Set the CA certificate path to load")
|
||||
|
||||
uploadCmd.Flags().StringP("path", "p", ".", "Set the path to list files (can be set with MAKESHIFT_PATH)")
|
||||
|
||||
uploadProfilesCmd.Flags().VarP(&inputFormat, "format", "F", "Set the input format for profile")
|
||||
|
|
@ -220,31 +261,27 @@ func processFiles(args []string) map[string][]byte {
|
|||
// load data either from file or directly from args
|
||||
var collection = make(map[string][]byte, len(args))
|
||||
for _, arg := range args {
|
||||
// if arg is empty string, then skip and continue
|
||||
// skip empty string args
|
||||
if len(arg) > 0 {
|
||||
// determine if we're reading from file to load contents
|
||||
if strings.HasPrefix(arg, "@") {
|
||||
var (
|
||||
path string = strings.TrimLeft(arg, "@")
|
||||
contents []byte
|
||||
err error
|
||||
)
|
||||
contents, err = os.ReadFile(path)
|
||||
var path string = strings.TrimLeft(arg, "@")
|
||||
|
||||
// process sub-directories recursively
|
||||
newCollection, err := processDir(path)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("path", path).Msg("failed to read file")
|
||||
continue
|
||||
log.Warn().
|
||||
Err(err).
|
||||
Str("path", path).
|
||||
Msg("failed to process directory at path")
|
||||
}
|
||||
log.Trace().
|
||||
Str("path", path).
|
||||
Msg("new collection added at path")
|
||||
maps.Copy(collection, newCollection)
|
||||
|
||||
// skip empty files
|
||||
if len(contents) == 0 {
|
||||
log.Warn().Str("path", path).Msg("file is empty")
|
||||
continue
|
||||
}
|
||||
|
||||
// add loaded data to collection of all data
|
||||
collection[path] = contents
|
||||
} else {
|
||||
log.Warn().Msg("only files can be uploaded (add @ before the path)")
|
||||
log.Warn().Msg("only files can be uploaded (add @ before the path with '--data' flag)")
|
||||
|
||||
continue
|
||||
}
|
||||
|
|
@ -275,20 +312,28 @@ func processProfiles(args []string) []*makeshift.Profile {
|
|||
)
|
||||
contents, err = os.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("path", path).Msg("failed to read file")
|
||||
log.Error().
|
||||
Err(err).
|
||||
Str("path", path).
|
||||
Msg("failed to read file")
|
||||
continue
|
||||
}
|
||||
|
||||
// skip empty files
|
||||
if len(contents) == 0 {
|
||||
log.Warn().Str("path", path).Msg("file is empty")
|
||||
log.Warn().
|
||||
Str("path", path).
|
||||
Msg("file is empty")
|
||||
continue
|
||||
}
|
||||
|
||||
// convert/validate input data
|
||||
data, err = parseProfile(contents, format.DataFormatFromFileExt(path, inputFormat))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("path", path).Msg("failed to validate input from file")
|
||||
log.Error().
|
||||
Err(err).
|
||||
Str("path", path).
|
||||
Msg("failed to validate input from file")
|
||||
}
|
||||
|
||||
// add loaded data to collection of all data
|
||||
|
|
@ -306,7 +351,9 @@ func processProfiles(args []string) []*makeshift.Profile {
|
|||
}
|
||||
err = json.Unmarshal(input, &data)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("failed to unmarshal input for argument %d", i)
|
||||
log.Error().
|
||||
Err(err).
|
||||
Msgf("failed to unmarshal input for argument %d", i)
|
||||
}
|
||||
return []*makeshift.Profile{data}
|
||||
}
|
||||
|
|
@ -315,6 +362,73 @@ func processProfiles(args []string) []*makeshift.Profile {
|
|||
return collection
|
||||
}
|
||||
|
||||
func processDir(path string) (map[string][]byte, error) {
|
||||
var (
|
||||
collection = map[string][]byte{}
|
||||
fileInfo os.FileInfo
|
||||
contents []byte
|
||||
err error
|
||||
)
|
||||
// determine if path is directory
|
||||
if fileInfo, err = os.Stat(path); err == nil {
|
||||
if fileInfo.IsDir() {
|
||||
filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
|
||||
if !d.IsDir() {
|
||||
contents, err = os.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("path", path).Msg("failed to read file")
|
||||
return nil
|
||||
}
|
||||
|
||||
// skip empty files
|
||||
if len(contents) == 0 {
|
||||
log.Warn().Str("path", path).Msg("file is empty")
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Debug().
|
||||
Str("path", path).
|
||||
Msg("file added to collection")
|
||||
|
||||
// add loaded data to collection of all data
|
||||
collection[path] = contents
|
||||
} else {
|
||||
// process sub-directories recursively
|
||||
newCollection, err := processDir(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to process directory at path '%s': %v", path, err)
|
||||
}
|
||||
log.Trace().
|
||||
Str("path", path).
|
||||
Msg("new collection added from nested directory")
|
||||
maps.Copy(collection, newCollection)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
} else {
|
||||
contents, err = os.ReadFile(path)
|
||||
if err != nil {
|
||||
return collection, fmt.Errorf("failed to read file at path '%s': %v", path, err)
|
||||
}
|
||||
|
||||
// skip empty files
|
||||
if len(contents) == 0 {
|
||||
return collection, fmt.Errorf("file is empty")
|
||||
}
|
||||
|
||||
log.Debug().
|
||||
Str("path", path).
|
||||
Msg("file added to collection")
|
||||
|
||||
// add loaded data to collection of all data
|
||||
collection[path] = contents
|
||||
}
|
||||
} else {
|
||||
return nil, fmt.Errorf("failed to stat file: %v", err)
|
||||
}
|
||||
return collection, nil
|
||||
}
|
||||
|
||||
func parseProfile(contents []byte, dataFormat format.DataFormat) (*makeshift.Profile, error) {
|
||||
var (
|
||||
data *makeshift.Profile
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue