github.com/sqlitebrowser/dio@v0.0.0-20240125125356-b587368e5c6b/cmd/licenceAdd.go (about) 1 package cmd 2 3 import ( 4 "errors" 5 "fmt" 6 "net/http" 7 "net/url" 8 "os" 9 10 rq "github.com/parnurzeal/gorequest" 11 "github.com/spf13/cobra" 12 ) 13 14 var licenceAddFile, licenceAddFileFormat, licenceAddFullName, licenceAddURL string 15 var licenceAddDisplayOrder int 16 17 // Adds a licence to the list of known licences on the server 18 var licenceAddCmd = &cobra.Command{ 19 Use: "add [licence name]", 20 Short: "Add a licence to the list of known licences on a DBHub.io cloud", 21 RunE: func(cmd *cobra.Command, args []string) error { 22 return licenceAdd(args) 23 }, 24 } 25 26 func init() { 27 licenceCmd.AddCommand(licenceAddCmd) 28 licenceAddCmd.Flags().IntVar(&licenceAddDisplayOrder, "display-order", 0, 29 "Used when displaying a list of available licences. This adjusts the position in the list.") 30 licenceAddCmd.Flags().StringVar(&licenceAddFileFormat, "file-format", "text", 31 "The content format of the file. Either text or html") 32 licenceAddCmd.Flags().StringVar(&licenceAddFullName, "full-name", "", 33 "The full name of the licence") 34 licenceAddCmd.Flags().StringVar(&licenceAddFile, "licence-file", "", 35 "Path to a file containing the licence as text") 36 licenceAddCmd.Flags().StringVar(&licenceAddURL, "source-url", "", 37 "Optional reference URL for the licence") 38 } 39 40 func licenceAdd(args []string) error { 41 // Ensure a short licence name is present 42 if len(args) == 0 { 43 return errors.New("A short licence name or identifier is needed. eg CC0-BY-1.0") 44 } 45 if len(args) > 1 { 46 return errors.New("Only one licence can be added at a time (for now)") 47 } 48 49 // Ensure a display order was specified 50 if licenceAddDisplayOrder == 0 { 51 return errors.New("A (unique) display order # must be given") 52 } 53 54 // Ensure a licence file was specified, and that it exists 55 if licenceAddFile == "" { 56 return errors.New("A file containing the licence text is required") 57 } 58 _, err := os.Stat(licenceAddFile) 59 if err != nil { 60 return err 61 } 62 63 // Send the licence info to the API server 64 name := args[0] 65 req := rq.New().TLSClientConfig(&TLSConfig).Post(fmt.Sprintf("%s/licence/add", cloud)). 66 Type("multipart"). 67 Query(fmt.Sprintf("licence_id=%s", url.QueryEscape(name))). 68 Query(fmt.Sprintf("display_order=%d", licenceAddDisplayOrder)). 69 Set("User-Agent", fmt.Sprintf("Dio %s", DIO_VERSION)). 70 SendFile(licenceAddFile, "", "file1") 71 if licenceAddFileFormat != "" { 72 req.Query(fmt.Sprintf("file_format=%s", url.QueryEscape(licenceAddFileFormat))) 73 } 74 if licenceAddFullName != "" { 75 req.Query(fmt.Sprintf("licence_name=%s", url.QueryEscape(licenceAddFullName))) 76 } 77 if licenceAddURL != "" { 78 req.Query(fmt.Sprintf("source_url=%s", url.QueryEscape(licenceAddURL))) 79 } 80 resp, body, errs := req.End() 81 if errs != nil { 82 _, err = fmt.Fprint(fOut, "Errors when adding licence:") 83 if err != nil { 84 return err 85 } 86 for _, errInner := range errs { 87 errTxt := errInner.Error() 88 _, errInnerInner := fmt.Fprint(fOut, errTxt) 89 if errInnerInner != nil { 90 return errInnerInner 91 } 92 } 93 return errors.New("Error when adding licence") 94 } 95 if resp.StatusCode != http.StatusCreated { 96 if resp.StatusCode == http.StatusConflict { 97 return errors.New(body) 98 } 99 100 return errors.New(fmt.Sprintf("Adding licence failed with an error: HTTP status %d - '%v'\n", 101 resp.StatusCode, resp.Status)) 102 } 103 104 _, err = fmt.Fprintf(fOut, "Licence '%s' added\n", name) 105 return err 106 }