From 13b02c03e8f13387d08c09c0dac4e13309f69a51 Mon Sep 17 00:00:00 2001 From: David Allen Date: Sun, 31 Aug 2025 00:13:33 -0600 Subject: [PATCH] feat: implemented upload cmd and pkg --- cmd/upload.go | 29 +++++++++++++++++++++++------ pkg/service/routes.go | 30 +++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/cmd/upload.go b/cmd/upload.go index ecdd638..d1a9757 100644 --- a/cmd/upload.go +++ b/cmd/upload.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "os" + "path/filepath" "strings" "git.towk2.me/towk/makeshift/internal/format" @@ -42,18 +43,37 @@ var uploadCmd = &cobra.Command{ }, Run: func(cmd *cobra.Command, args []string) { var ( + host, _ = cmd.Flags().GetString("host") + path, _ = cmd.Flags().GetString("path") dataArgs, _ = cmd.Flags().GetStringArray("data") inputData = processFiles(dataArgs) useDirectoryPath = len(inputData) > 1 + c = client.New(host) + res *http.Response + query string + err error ) - for path, contents := range inputData { + for inputPath, contents := range inputData { log.Info().Str("path", path).Int("size", len(contents)).Send() if useDirectoryPath { - + query = path + "/" + filepath.Clean(inputPath) } else { - + // use flag value if supplied + if cmd.Flags().Changed("path") { + query = path + } else { + query = inputPath + } } + + query = fmt.Sprintf("/upload/%s", query) + res, _, err = c.MakeRequest(client.HTTPEnvelope{ + Path: query, + Method: http.MethodPost, + Body: contents, + }) + handleResponseError(res, host, query, err) } }, } @@ -70,10 +90,7 @@ var uploadProfilesCmd = &cobra.Command{ Args: cobra.NoArgs, Short: "Upload a new profile", Run: func(cmd *cobra.Command, args []string) { - - // make one request be host positional argument (restricted to 1 for now) var ( - // inputData []map[string]any = append(handleArgs(args), processDataArgs(dataArgs)...) host, _ = cmd.Flags().GetString("host") dataArgs, _ = cmd.Flags().GetStringArray("data") profiles = processProfiles(dataArgs) diff --git a/pkg/service/routes.go b/pkg/service/routes.go index 23d2d05..b5dd890 100644 --- a/pkg/service/routes.go +++ b/pkg/service/routes.go @@ -3,6 +3,7 @@ package service import ( "encoding/json" "fmt" + "io" "io/fs" "net/http" "os" @@ -182,9 +183,36 @@ func (s *Service) Download() http.HandlerFunc { func (s *Service) Upload() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var ( - _ = s.PathForData() + strings.TrimPrefix(r.URL.Path, "/upload") + path = s.PathForData() + strings.TrimPrefix(r.URL.Path, "/upload") + body []byte + dirpath string + err error ) + // show what we're uploading + log.Debug().Str("path", path).Msg("Service.Upload()") + + // take the provided path and store the file contents + dirpath = filepath.Dir(path) + err = os.MkdirAll(dirpath, 0o777) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + // write file to disk + body, err = io.ReadAll(r.Body) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + err = os.WriteFile(path, body, 0o777) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusOK) } }