Skip to content

Commit

Permalink
feat: basic get value api
Browse files Browse the repository at this point in the history
  • Loading branch information
afeiship committed Nov 20, 2022
1 parent 178e426 commit 3b8ba8e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 7 deletions.
18 changes: 16 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"lcc/misc"
"os"
)

Expand All @@ -11,7 +13,17 @@ var rootCmd = &cobra.Command{
Short: "Leancloud cli for options curd.",
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Hello, world!")
// get optionKey
optionKey, _ := cmd.Flags().GetString("optionKey")
// Get value by optionKey
if optionKey != "" {
fmt.Println("optionKey:", optionKey)
res := misc.LcGet(optionKey)
fmt.Println("res:", res)
}
},
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -32,5 +44,7 @@ func init() {

// 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")
// get value by optionKey
//rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.Flags().StringP("optionKey", "k", "", "Get value by option key")
}
76 changes: 71 additions & 5 deletions misc/index.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,76 @@
package misc

import "github.com/go-resty/resty/v2"
import (
"encoding/json"
"github.com/go-resty/resty/v2"
"log"
"os"
)

func GetLc(key) {
type LcResponseBody struct {
Key string `json:"key"`
Value string `json:"value"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
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)
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
}

// 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 LcPut(optionKey string, key, value string) LcResponseBody {
url := GetUrl(optionKey)
client := resty.New()
resp, err := client.R().SetBody(map[string]string{
"key": key,
"value": value,
}).Put(url)
if err != nil {
log.Fatalln(err)
}
var lcResponseBody LcResponseBody
json.Unmarshal(resp.Body(), &lcResponseBody)
return lcResponseBody
}

func LcDelete(optionKey string) {
url := GetUrl(optionKey)
client := resty.New()
// format url
// url := "https://8alqie7r.lc-cn-n1-shared.com/1.1/classes/options/" + key

_, err := client.R().Delete(url)
if err != nil {
log.Fatalln(err)
}
}

0 comments on commit 3b8ba8e

Please sign in to comment.