Skip to content

Commit

Permalink
feat: r/u done
Browse files Browse the repository at this point in the history
  • Loading branch information
afeiship committed Nov 20, 2022
1 parent 3b8ba8e commit 0d82d61
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 40 deletions.
31 changes: 24 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,24 @@ var rootCmd = &cobra.Command{
// Uncomment the following line if your bare application
// has an action associated with it:
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Hello, world!")
// get optionKey
optionKey, _ := cmd.Flags().GetString("optionKey")
// get optionKey from args
optionKey := args[0]
isRead, _ := cmd.Flags().GetBool("read")
isUpdate, _ := cmd.Flags().GetBool("update")

// Get value by optionKey
if optionKey != "" {
fmt.Println("optionKey:", optionKey)
res := misc.LcGet(optionKey)
fmt.Println("res:", res)
// call by method:
// lcc read -k optionKey
if isRead {
fmt.Println(misc.LcRead(optionKey))
}

// lcc update -k optionKey -v key -v value
if isUpdate {
value, _ := cmd.Flags().GetString("value")
fmt.Println(misc.LcUpdate(optionKey, value))
}
}
},
}
Expand All @@ -46,5 +56,12 @@ func init() {
// when this action is called directly.
// get value by optionKey
//rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.Flags().StringP("optionKey", "k", "", "Get value by option key")
//rootCmd.Flags().StringP("optionKey", "k", "", "Get value by option key")
// add boolean create/update/read/delete flags
rootCmd.Flags().BoolP("read", "r", false, "Read option")
rootCmd.Flags().BoolP("update", "u", false, "Update option")

// args for optionKey
rootCmd.Flags().StringP("value", "v", "", "Set value by option key")

}
46 changes: 13 additions & 33 deletions misc/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,23 @@ type LcResponseBody struct {
ObjectId string `json:"objectId"`
}

func GetUrl(key string) string {
return "https://8alqie7r.lc-cn-n1-shared.com/1.1/classes/options/" + key
}

func LcGet(optionKey string) LcResponseBody {
url := GetUrl(optionKey)
func getRestClient() *resty.Client {
client := resty.New()
// set header Lc_key, lc_id from os.get env
lcId := os.Getenv("LC_ID")
lcKey := os.Getenv("LC_KEY")
client.SetHeaders(map[string]string{"X-LC-Id": lcId, "X-LC-Key": lcKey})
resp, err := client.R().Get(url)
if err != nil {
log.Fatalln(err)
}
var lcResponseBody LcResponseBody
json.Unmarshal(resp.Body(), &lcResponseBody)
return lcResponseBody
return client
}

// create item by key/value
func LcPost(optionKey string, key, value string) LcResponseBody {
url := GetUrl(optionKey)
client := resty.New()
resp, err := client.R().SetBody(map[string]string{
"key": key,
"value": value,
}).Post(url)
if err != nil {
log.Fatalln(err)
}
var lcResponseBody LcResponseBody
json.Unmarshal(resp.Body(), &lcResponseBody)
return lcResponseBody
func GetUrl(key string) string {
return "https://8alqie7r.lc-cn-n1-shared.com/1.1/classes/options/" + key
}

func LcPut(optionKey string, key, value string) LcResponseBody {
func LcUpdate(optionKey string, value string) LcResponseBody {
url := GetUrl(optionKey)
client := resty.New()
client := getRestClient()
resp, err := client.R().SetBody(map[string]string{
"key": key,
"value": value,
}).Put(url)
if err != nil {
Expand All @@ -66,11 +42,15 @@ func LcPut(optionKey string, key, value string) LcResponseBody {
return lcResponseBody
}

func LcDelete(optionKey string) {
func LcRead(optionKey string) string {
url := GetUrl(optionKey)
client := resty.New()
_, err := client.R().Delete(url)
client := getRestClient()
resp, err := client.R().Get(url)
if err != nil {
log.Fatalln(err)
}
var lcResponseBody LcResponseBody
json.Unmarshal(resp.Body(), &lcResponseBody)
// to json
return lcResponseBody.Value
}

0 comments on commit 0d82d61

Please sign in to comment.