From 64bf852aedc4f48c2efc30ff390aa9cbb169ac1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Sat, 26 Jun 2021 02:58:07 +0200 Subject: [PATCH] git: Upload artifacts --- api/api.go | 36 ++++++++++++++++++++++++++++------- api/git.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ cmd/git.go | 20 ++++++++++++++++++-- config/config.go | 3 --- 4 files changed, 96 insertions(+), 12 deletions(-) diff --git a/api/api.go b/api/api.go index 7204311..a275346 100644 --- a/api/api.go +++ b/api/api.go @@ -4,9 +4,12 @@ import ( "bytes" "encoding/json" "fmt" + "io" "io/ioutil" + "mime/multipart" "net/http" - "strings" + "os" + "path/filepath" "time" "git.xenrox.net/~xenrox/srhtctl/config" @@ -95,17 +98,36 @@ func Request(url string, method string, body interface{}, response ...interface{ return nil } -// FormRequest does an API request with x-www-form-urlencoded -func FormRequest(url string, method string, annotations string, response ...interface{}) error { +// UploadFileRequest uploads a file +func UploadFileRequest(url string, fileName string, response ...interface{}) error { client := &http.Client{Timeout: 3 * time.Second} - req, err := http.NewRequest(method, url, strings.NewReader(annotations)) + file, err := os.Open(fileName) if err != nil { return err } - req.Header.Add("Content-Type", "x-www-form-urlencoded") - token := config.GetConfigValue("settings", "token") - req.Header.Add("Content-Type", "application/json") + defer file.Close() + + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + // Submit your request as multipart/form-data, with a single field: "file". + part, err := writer.CreateFormFile("file", filepath.Base(file.Name())) + if err != nil { + return err + } + + _, err = io.Copy(part, file) + if err != nil { + return err + } + writer.Close() + + req, err := http.NewRequest("POST", url, body) + if err != nil { + return err + } + token := config.GetToken() + req.Header.Add("Content-Type", writer.FormDataContentType()) req.Header.Add("Authorization", fmt.Sprintf("token %s", token)) resp, err := client.Do(req) if err != nil { diff --git a/api/git.go b/api/git.go index 778f64e..365a59a 100644 --- a/api/git.go +++ b/api/git.go @@ -1 +1,50 @@ package api + +import ( + "fmt" + + "git.xenrox.net/~xenrox/srhtctl/config" + "git.xenrox.net/~xenrox/srhtctl/helpers" +) + +// RepoName is the name of a git repository +var RepoName string + +// RefName is the name of a git repository ref +var RefName string + +// ArtifactsUpload uploads artifact(s) to a repo ref +func ArtifactsUpload(args []string) error { + var repoName string + var refName string + if RepoName == "" || RefName == "" { + // TODO: Try to parse from current git repo + fmt.Println("You have to set a repository and a ref") + } else { + repoName = RepoName + refName = RefName + } + + for _, artifact := range args { + err := artifactUpload(repoName, refName, artifact) + if err != nil { + return err + } + } + return nil +} + +// artifactUpload uploads a single artifact to a repo ref +func artifactUpload(repoName string, refName, fileName string) error { + var url string + if UserName != "" { + url = fmt.Sprintf("%s/%s/api/repos/%s/artifacts/%s", config.GetURL("git"), + helpers.TransformCanonical(UserName), repoName, refName) + } else { + url = fmt.Sprintf("%s/api/repos/%s/artifacts/%s", config.GetURL("git"), + repoName, refName) + } + + err := UploadRequest(url, "POST", fileName) + return err +} diff --git a/cmd/git.go b/cmd/git.go index 7b701b3..60a4375 100644 --- a/cmd/git.go +++ b/cmd/git.go @@ -1,7 +1,8 @@ package cmd import ( - "git.xenrox.net/~xenrox/srhtctl/config" + "git.xenrox.net/~xenrox/srhtctl/api" + "git.xenrox.net/~xenrox/srhtctl/helpers/errorhelper" "github.com/spf13/cobra" ) @@ -12,7 +13,22 @@ var gitCmd = &cobra.Command{ }, } +var artifactsCmd = &cobra.Command{ + Use: "artifacts", + Short: "Upload artifact(s) to a ref", + Args: cobra.MinimumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + err := api.ArtifactsUpload(args) + errorhelper.ExitError(err) + }, +} + func init() { rootCmd.AddCommand(gitCmd) - gitCmd.PersistentFlags().StringVarP(&config.UserName, "user", "u", "", "Git user (without ~)") + gitCmd.PersistentFlags().StringVarP(&api.RepoName, "repo", "r", "", "Repository name") + gitCmd.RegisterFlagCompletionFunc("repo", completeNoFiles) + + gitCmd.AddCommand(artifactsCmd) + artifactsCmd.Flags().StringVar(&api.RefName, "ref", "", "Ref name") + artifactsCmd.RegisterFlagCompletionFunc("ref", completeNoFiles) } diff --git a/config/config.go b/config/config.go index 279ddbe..c82b2a2 100644 --- a/config/config.go +++ b/config/config.go @@ -13,9 +13,6 @@ import ( // ConfigPath is a path to a non default config.ini file var ConfigPath string -// UserName is the sr.ht user name without ~ -var UserName string - var configFile *ini.File // LoadConfig parses the config.ini file and returns it. -- 2.44.0