M api/api.go => api/api.go +29 -7
@@ 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 {
M api/git.go => api/git.go +49 -0
@@ 1,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
+}
M cmd/git.go => cmd/git.go +18 -2
@@ 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)
}
M config/config.go => config/config.go +0 -3
@@ 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.