Skip to content

Commit

Permalink
feat: Support alternative transfer.sh instances
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFantom committed Feb 17, 2023
1 parent cf07845 commit e51bb5e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions cmd/sshare/sshare.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

var (
tshInstanceURL string = transfer.DefaultTransferBaseURL
transferDownloads int = 0
transferDays int = 0
sshAgentPath string = os.Getenv("SSH_AUTH_SOCK")
Expand All @@ -33,6 +34,9 @@ var (
if transferDownloads <= 0 {
ui.Errorln("Uploaded content must be downloadable via transfer.sh for at least 1 time", true)
}
if err := transfer.SetTransferShURL(tshInstanceURL); err != nil {
ui.Errorln("Transfer.sh URL is not valid: "+err.Error(), true)
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -139,6 +143,7 @@ func init() {
rootCmd.Flags().StringVarP(&githubToken, "github-token", "g", githubToken, "github token with permission to read ssh keys")
rootCmd.Flags().StringSliceVarP(&keyFilepaths, "key-file", "f", keyFilepaths, "additional key file(s) to include in the generated authorized_keys")
rootCmd.Flags().StringSliceVarP(&rawKeys, "key", "k", rawKeys, "additional keys to include in the generated authorized_keys")
rootCmd.PersistentFlags().StringVar(&tshInstanceURL, "url", transfer.DefaultTransferBaseURL, "url of the target transfer.sh instance")
rootCmd.Flags().IntVarP(&transferDownloads, "max-downloads", "m", 10, "maximum number of times any content shared can be downloaded")
rootCmd.Flags().IntVarP(&transferDays, "max-days", "d", 2, "number of days that the content will remain available via transfer.sh")
rootCmd.AddCommand(deleteCmd)
Expand Down
21 changes: 18 additions & 3 deletions internal/transfer/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,24 @@ type TransferSh struct {

const (
// TransferBaseURL is the base URL of transfer.sh using https.
TransferBaseURL string = "https://transfer.sh"
DefaultTransferBaseURL string = "https://transfer.sh"
)

var (
transferBaseURL string = DefaultTransferBaseURL
)

func SetTransferShURL(tshURL string) error {
if u, err := url.Parse(tshURL); err != nil {
return fmt.Errorf("invalid transfer.sh url: %w", err)
} else if u.Scheme != "http" && u.Scheme != "https" {
return fmt.Errorf("transfer.sh url not http or https scheme")
} else {
transferBaseURL = u.String()
}
return nil
}

// NewTransferSh returns an instance of a TransferSh config with appropriate
// default value for the context. Specifically, the file name is set to
// authorized_keys with a max of 10 downloads and a life of 2 days.
Expand Down Expand Up @@ -57,7 +72,7 @@ func (tsh TransferSh) WithMaxDays(maxDays int) TransferSh {
// transfer.sh. Returned are the URLs to both download/curl the file and to
// delete the file form transfer.sh. If the upload fails, an error is returned.
func (tsh TransferSh) Upload(data string) (*TransferShFile, error) {
uploadURL, err := url.JoinPath(TransferBaseURL, tsh.fileName)
uploadURL, err := url.JoinPath(transferBaseURL, tsh.fileName)
if err != nil {
return nil, fmt.Errorf("failed to build upload url for transfer.sh: %w", err)
}
Expand Down Expand Up @@ -86,5 +101,5 @@ func (tsh TransferSh) Upload(data string) (*TransferShFile, error) {
if deleteURL == "" || downloadURL == "" {
return nil, fmt.Errorf("failed to obtain all urls for the transfer.sh upload")
}
return NewFile(downloadURL, path.Base(deleteURL)), nil
return NewFile(downloadURL, path.Base(deleteURL)), nil
}

0 comments on commit e51bb5e

Please sign in to comment.