Skip to content

Commit

Permalink
build: move OTS base URL into build config
Browse files Browse the repository at this point in the history
  • Loading branch information
rickymarcon committed Aug 11, 2021
1 parent 49eb69f commit ce82621
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 23 deletions.
6 changes: 4 additions & 2 deletions api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"time"

"github.com/sniptt-official/ots/build"
"github.com/spf13/viper"
)

type CreateOtsReq struct {
Expand All @@ -40,6 +41,8 @@ type CreateOtsRes struct {
}

func CreateOts(encryptedBytes []byte, expiresIn time.Duration) (*CreateOtsRes, error) {
baseUrl := viper.GetString("base_url")

reqBody := &CreateOtsReq{
EncryptedBytes: base64.StdEncoding.EncodeToString(encryptedBytes),
ExpiresIn: uint32(expiresIn.Seconds()),
Expand All @@ -55,8 +58,7 @@ func CreateOts(encryptedBytes []byte, expiresIn time.Duration) (*CreateOtsRes, e

client := &http.Client{}

// TODO: Make URL part of config
req, err := http.NewRequest("POST", "https://api.ots.sniptt.com/secrets", payloadBuf)
req, err := http.NewRequest("POST", baseUrl, payloadBuf)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ package build

// Will be changed at build time via -ldflags
var Version = "debug"
var BaseUrl = "https://api.ots.sniptt.com/secrets"
2 changes: 1 addition & 1 deletion cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const (
var (
expires time.Duration

// newCmd represents the new command
// newCmd represents the new command.
newCmd = &cobra.Command{
Use: "new",
Short: "Create end-to-end encrypted secret",
Expand Down
25 changes: 5 additions & 20 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,8 @@ var (

// rootCmd represents the base command when called without any subcommands
rootCmd = &cobra.Command{
Use: "ots",
Short: "Easily create and share end-to-end encrypted secrets with others",
// Long: `A longer description that spans multiple lines and likely contains
// examples and usage of using your application. For example:

// Cobra is a CLI library for Go that empowers applications.
// This application is a tool to generate the needed files
// to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Use: "ots",
Short: "Easily create and share end-to-end encrypted secrets with others",
Version: build.Version,
}
)
Expand All @@ -53,15 +44,7 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.ots.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

// initConfig reads in config file and ENV variables if set.
Expand All @@ -78,9 +61,11 @@ func initConfig() {
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".ots")
viper.SetDefault("base_url", build.BaseUrl)
}

viper.AutomaticEnv() // read in environment variables that match
// Read in environment variables that match.
viper.AutomaticEnv()

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
Expand Down

0 comments on commit ce82621

Please sign in to comment.