Skip to content

Commit

Permalink
upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
nj-jay committed Dec 7, 2020
1 parent 86d8522 commit 58dff81
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 77 deletions.
25 changes: 12 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@

## 使用

修改配置文件
chmod +x /path/to/qn

~/.qn.toml
export PATH = "/path/to/qn:$PATH"

增加如下内容
```
ak = ""
sk = ""
bucket = ""
设置ak sk bucket
qn account -w ak sk bucket

```
## 查看帮助

chmod +x qn
qn -h

把qn添加到环境变量中
export PATH = "/path/to/qn:$PATH"
## 目前支持的命令

## 查看帮助
* put
> qn put filepath
qn -h
## TODO

支持通配符
48 changes: 42 additions & 6 deletions cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,26 @@ limitations under the License.
package cmd

import (

"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (

accountOver bool

)

// accountCmd represents the account command
var accountCmd = &cobra.Command{
Use: "account",
Short: "get account information",
Long: `Get AccessKey and SecretKey`,
Use: "account [<AccessKey> <SecretKey> <Bucket>]",
Short: "get/set account information",
Long: `Get/Set AccessKey and SecretKey and Bucket`,
Args: func(cmd *cobra.Command, args []string) error {

if len(args) != 0 {
if len(args) != 0 && len(args) != 3 {

return fmt.Errorf("command account receives zero or four args, received %d\n", len(args))
}
Expand All @@ -52,22 +59,51 @@ func Account(cmd *cobra.Command, params []string) {

if ak== "" || sk== "" || bucket == "" {

fmt.Println("请在配置文件中设置你的ak, sk, bucket")
fmt.Println("please set your ak, sk, bucket in config file(default is ~/.qn.toml)")

} else {

fmt.Println("Reading from ~/.qn.toml....")
fmt.Println("ak: ", ak)
fmt.Println("sk: ", sk)
fmt.Println("bucket ", bucket)
fmt.Println("bucket: ", bucket)
}

} else if len(params) == 3 && accountOver {

viper.Set("ak", params[0])

viper.Set("sk", params[1])

viper.Set("bucket", params[2])

err := viper.WriteConfig()

if err != nil {

fmt.Println("write config failed: ", err)
}

} else if len(params) == 3 && !accountOver {


fmt.Println("please use -w flags. For example, qn account -w ak sk bucket")

}

}

func init() {

accountCmd.Flags().BoolVarP(
&accountOver,
"overwrite",
"w",
false,
"overwrite account or not when account exists in local config file(default ~/.qn.toml), by default not overwrite",

)

rootCmd.AddCommand(accountCmd)

}
48 changes: 33 additions & 15 deletions cmd/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,22 @@ import (

// putCmd represents the put command
var putCmd = &cobra.Command{
Use: "put",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. 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.`,
Use: "put <local file> [flags]",
Short: "put file to qiniu server",
Long: `use put command, you can put your local file to qiniu server, and get a url.`,
Run: put,
}

func put(cmd *cobra.Command, params []string){

if len(params) <= 0 {

fmt.Println("请填写图片路径")
fmt.Println("please set filepath...")

os.Exit(0)

}

//fmt.Println(params)

path := params[0]

index := strings.LastIndexAny(path, "/")
Expand All @@ -67,39 +61,63 @@ func put(cmd *cobra.Command, params []string){
}

accessKey := viper.GetString("ak")

secretKey := viper.GetString("sk")

bucket := viper.GetString("bucket")

//fmt.Println(accessKey)
//
//fmt.Println(secretKey)
//
//fmt.Println(bucket)

putPolicy := storage.PutPolicy{

Scope: bucket,
}

mac := qbox.NewMac(accessKey, secretKey)

upToken := putPolicy.UploadToken(mac)

cfg := storage.Config{}

//bm := storage.NewBucketManager(mac, &cfg)
//
//domains, err := bm.ListBucketDomains(bucket)
//
//fmt.Println(domains)

formUploader := storage.NewFormUploader(&cfg)

ret := storage.PutRet{}

putExtra := storage.PutExtra{

Params: map[string]string{

"x:name": "picture or some other data",
},
}


err := formUploader.PutFile(context.Background(), &ret, upToken, upload, path, &putExtra)

if err != nil {

fmt.Println("上传失败")

return
}
fmt.Println("上传成功","外链如下")
fmt.Println("https://picture.hs-vae.com/" + ret.Key)

fmt.Println("upload successfully")

}


func init() {

rootCmd.AddCommand(putCmd)



}
2 changes: 0 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ func initConfig() {

cfgFile = homeDir + "/.qn.toml"

//fmt.Println(cfgFile)

if hErr != nil {

fmt.Printf("get current home directory: %v\n", hErr)
Expand Down
41 changes: 0 additions & 41 deletions cmd/version.go

This file was deleted.

Binary file modified qn
Binary file not shown.

0 comments on commit 58dff81

Please sign in to comment.