Skip to content

Commit 66e3c30

Browse files
authored
build: move ots base url into build config (#22)
1 parent b5f77ef commit 66e3c30

File tree

4 files changed

+11
-23
lines changed

4 files changed

+11
-23
lines changed

api/client/client.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"time"
2727

2828
"github.com/sniptt-official/ots/build"
29+
"github.com/spf13/viper"
2930
)
3031

3132
type CreateOtsReq struct {
@@ -40,6 +41,8 @@ type CreateOtsRes struct {
4041
}
4142

4243
func CreateOts(encryptedBytes []byte, expiresIn time.Duration) (*CreateOtsRes, error) {
44+
baseUrl := viper.GetString("base_url")
45+
4346
reqBody := &CreateOtsReq{
4447
EncryptedBytes: base64.StdEncoding.EncodeToString(encryptedBytes),
4548
ExpiresIn: uint32(expiresIn.Seconds()),
@@ -55,8 +58,7 @@ func CreateOts(encryptedBytes []byte, expiresIn time.Duration) (*CreateOtsRes, e
5558

5659
client := &http.Client{}
5760

58-
// TODO: Make URL part of config
59-
req, err := http.NewRequest("POST", "https://api.ots.sniptt.com/secrets", payloadBuf)
61+
req, err := http.NewRequest("POST", baseUrl, payloadBuf)
6062
if err != nil {
6163
return nil, err
6264
}

build/build.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ package build
1717

1818
// Will be changed at build time via -ldflags
1919
var Version = "debug"
20+
var BaseUrl = "https://api.ots.sniptt.com/secrets"

cmd/new.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const (
3838
var (
3939
expires time.Duration
4040

41-
// newCmd represents the new command
41+
// newCmd represents the new command.
4242
newCmd = &cobra.Command{
4343
Use: "new",
4444
Short: "Create end-to-end encrypted secret",

cmd/root.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,8 @@ var (
2929

3030
// rootCmd represents the base command when called without any subcommands
3131
rootCmd = &cobra.Command{
32-
Use: "ots",
33-
Short: "Easily create and share end-to-end encrypted secrets with others",
34-
// Long: `A longer description that spans multiple lines and likely contains
35-
// examples and usage of using your application. For example:
36-
37-
// Cobra is a CLI library for Go that empowers applications.
38-
// This application is a tool to generate the needed files
39-
// to quickly create a Cobra application.`,
40-
// Uncomment the following line if your bare application
41-
// has an action associated with it:
42-
// Run: func(cmd *cobra.Command, args []string) { },
32+
Use: "ots",
33+
Short: "Easily create and share end-to-end encrypted secrets with others",
4334
Version: build.Version,
4435
}
4536
)
@@ -53,15 +44,7 @@ func Execute() {
5344
func init() {
5445
cobra.OnInitialize(initConfig)
5546

56-
// Here you will define your flags and configuration settings.
57-
// Cobra supports persistent flags, which, if defined here,
58-
// will be global for your application.
59-
6047
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.ots.yaml)")
61-
62-
// Cobra also supports local flags, which will only run
63-
// when this action is called directly.
64-
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
6548
}
6649

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

83-
viper.AutomaticEnv() // read in environment variables that match
67+
// Read in environment variables that match.
68+
viper.AutomaticEnv()
8469

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

0 commit comments

Comments
 (0)