Skip to content

Commit

Permalink
add more json flags
Browse files Browse the repository at this point in the history
  • Loading branch information
YieldRay committed Feb 26, 2024
1 parent 4af05f9 commit 8bc5f19
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 26 deletions.
17 changes: 5 additions & 12 deletions api/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bufio"
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
"io/fs"
Expand All @@ -25,10 +24,13 @@ import (
// onEventStream <jsonString=>void>
func Upload(client *http.Client, token, domain, src string, onEventStream func(byteLine []byte)) (err error) {
if !utils.IsDir(src) {
return errors.New("not a directory")
return fmt.Errorf("%s is not a directory", src)
}
// 获取绝对路径,保证tar是压缩了一个文件夹而不是其内容(当src为当前目录时)
src, _ = filepath.Abs(src)
src, err = filepath.Abs(src)
if err != nil {
return err
}

computeTarPath := computeTarPathFn(src)
computeIgnore := computeIgnoreFn(src)
Expand Down Expand Up @@ -138,13 +140,6 @@ func Upload(client *http.Client, token, domain, src string, onEventStream func(b
return err
}
return fmt.Errorf("%s", b)

// TODO:读取返回的json再封装为error

// m := make(map[string]interface{})
// if err = json.Unmarshal(b, &m); err != nil {
// return errors.New(res.Status)
// }
}

reader := bufio.NewReader(res.Body)
Expand All @@ -154,9 +149,7 @@ func Upload(client *http.Client, token, domain, src string, onEventStream func(b
if err == io.EOF {
break
}

onEventStream(line)

}

return nil
Expand Down
15 changes: 15 additions & 0 deletions cli/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ import (
"github.com/charmbracelet/huh/spinner"
"github.com/urfave/cli/v2"
"github.com/yieldray/surgecli/types"
"github.com/yieldray/surgecli/utils"
)

func init() {
var isJSON int

Commands = append(Commands,
&cli.Command{
Name: "account",
Aliases: []string{"plan"},
Usage: "Show account information",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "json",
Usage: "Only print JSON",
Count: &isJSON,
}},
Action: func(cCtx *cli.Context) error {

if email := surgesh.Whoami(); email == "" {
Expand All @@ -32,6 +41,12 @@ func init() {
return err
}

// only print json
if isJSON > 0 {
fmt.Print(utils.JSONStringify(acc))
return nil
}

fmt.Printf("%-6s: %s\n", "Email", acc.Email)
fmt.Printf("%-6s: %s\n", "ID", acc.ID)
fmt.Printf("%-6s: %s\n", "UUID", acc.UUID)
Expand Down
20 changes: 16 additions & 4 deletions cli/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

func init() {
var isShort int
var isJSON int

Commands = append(Commands,
&cli.Command{
Expand All @@ -22,6 +23,10 @@ func init() {
Name: "short",
Usage: "Only print domain list",
Count: &isShort,
}, &cli.BoolFlag{
Name: "json",
Usage: "Only print JSON",
Count: &isJSON,
}},
Action: func(cCtx *cli.Context) error {
if email := surgesh.Whoami(); email == "" {
Expand All @@ -38,6 +43,13 @@ func init() {
if err != nil {
return err
} else {

// only print json
if isJSON > 0 {
fmt.Print(utils.JSONStringify(list))
return nil
}

// only print domain
if isShort > 0 {
for _, site := range list {
Expand All @@ -47,11 +59,11 @@ func init() {
}

// print full info
s := func(fc int) string {
if fc == 1 {
return fmt.Sprintf("%d file", fc)
s := func(count int) string {
if count == 1 {
return fmt.Sprintf("%d file", count)
}
return fmt.Sprintf("%d files", fc)
return fmt.Sprintf("%d files", count)
}

for _, site := range list {
Expand Down
10 changes: 2 additions & 8 deletions cli/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,13 @@ func init() {
fmt.Println("To setup custom domain, see")
fmt.Print("https://surge.world/ \nhttps://surge.sh/help/adding-a-custom-domain")
return nil
} else {
if !utils.IsDir(dir) {
return fmt.Errorf("%s is not a directory", dir)
}
}

domain := cCtx.Args().Get(1)
cnameFilePath := path.Join(dir, "CNAME")

if domain == "" {
b, _ := os.ReadFile(cnameFilePath)
domain = strings.Trim(string(b), " ")
domain, _ = utils.ReadTextFileTrim(cnameFilePath)
}

if domain == "" {
Expand Down Expand Up @@ -129,8 +124,7 @@ func onUploadEvent(byteLine []byte, isSilent bool, isJSON bool) {
json.Unmarshal(byteLine, &p)

if isJSON {
j, _ := json.MarshalIndent(p, "", " ")
fmt.Print(string(j))
fmt.Print(utils.JSONStringify(p))
} else {
for _, url := range p.Urls {
fmt.Printf("[%-14s]\nhttps://%s\n", pad14(url.Name), url.Domain)
Expand Down
1 change: 1 addition & 0 deletions surgecli.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ func main() {

if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
23 changes: 21 additions & 2 deletions utils/file.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package utils

import "os"
import (
"os"
"strings"
)

func IsDir(f string) bool {
fi, err := os.Stat(f)
return !os.IsNotExist(err) && fi.IsDir()
return err == nil && fi.IsDir()
}

func IsFileExist(path string) bool {
Expand All @@ -20,3 +23,19 @@ func IsFileExist(path string) bool {
return s.Mode().IsRegular()
}
}

func ReadTextFile(name string) (string, error) {
b, err := os.ReadFile(name)
if err != nil {
return "", err
}
return string(b), nil
}

func ReadTextFileTrim(name string) (string, error) {
str, err := ReadTextFile(name)
if err != nil {
return "", err
}
return strings.Trim(str, " "), nil
}
6 changes: 6 additions & 0 deletions utils/fmt.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"encoding/json"
"fmt"
"strconv"
)
Expand All @@ -17,3 +18,8 @@ func FormatBytes(bytes int64) string {
}
return fmt.Sprintf("%.2f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
}

func JSONStringify(v any) string {
j, _ := json.MarshalIndent(v, "", " ")
return (string(j))
}

0 comments on commit 8bc5f19

Please sign in to comment.