Skip to content

Commit 4358b07

Browse files
committed
fix
1 parent ca5f93b commit 4358b07

File tree

11 files changed

+355
-1
lines changed

11 files changed

+355
-1
lines changed

cmd/clidemo/main.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/alecthomas/kong"
7+
)
8+
9+
var CLI struct {
10+
Ver string `default:"${version}"`
11+
Logging struct {
12+
Level string `enum:"debug,info,warn,error" default:"info" env:"Y" help:"Paths to remove."`
13+
Type string `enum:"json,console" default:"console" env:"X" help:"Paths to remove."`
14+
} `embed:""` //`embed:"" prefix:"logging." envprefix:"XXX_"`
15+
16+
Logging1 struct {
17+
Level string `enum:"debug,info,warn,error" default:"info" env:"Y" help:"Paths to remove."`
18+
Type string `enum:"json,console" default:"console" env:"X" help:"Paths to remove."`
19+
Type1 string `kong:"-"` // Ignore the field
20+
} `embed:"" prefix:"logging." envprefix:"XXX_"`
21+
Debug bool `help:"Enable debug mode." env:"X"`
22+
Rm struct {
23+
Force bool `help:"Force removal." env:"X"`
24+
Recursive bool `help:"Recursively remove files."`
25+
26+
Paths []string `arg:"" name:"path" help:"Paths to remove." type:"path"`
27+
} `cmd:"" help:"Remove files."`
28+
29+
Ls struct {
30+
Paths []string `arg:"" optional:"" name:"path" help:"Paths to list." type:"path"`
31+
} `cmd:"" help:"List paths."`
32+
33+
Help helpCmd `cmd:"" help:"Show help."`
34+
Question helpCmd `cmd:"" hidden:"" name:"?" help:"Show help."`
35+
Status statusCmd `cmd:"" help:"Show server status."`
36+
}
37+
38+
func main() {
39+
ctx := kong.Parse(
40+
&CLI,
41+
kong.Name("test"),
42+
//kong.NoDefaultHelp(),
43+
kong.Description("An app demonstrating HelpProviders"),
44+
kong.UsageOnError(),
45+
kong.Vars{
46+
"version": "0.0.1",
47+
},
48+
kong.ConfigureHelp(kong.HelpOptions{
49+
Compact: true,
50+
Summary: true,
51+
}),
52+
)
53+
ctx.FatalIfErrorf(ctx.Run())
54+
//assert.Must(ctx.PrintUsage(true))
55+
//switch ctx.Command() {
56+
//case "rm <path>":
57+
//case "ls":
58+
//default:
59+
//}
60+
}
61+
62+
type statusCmd struct {
63+
Verbose bool `short:"v" help:"Show verbose status information."`
64+
}
65+
66+
func (s *statusCmd) Run(ctx *kong.Context) error {
67+
//ctx.Printf("OK")
68+
fmt.Println("OK")
69+
return nil
70+
}
71+
72+
type helpCmd struct {
73+
Command []string `arg:"" optional:"" help:"Show help on command."`
74+
}
75+
76+
// Run shows help.
77+
func (h *helpCmd) Run(realCtx *kong.Context) error {
78+
ctx, err := kong.Trace(realCtx.Kong, h.Command)
79+
if err != nil {
80+
return err
81+
}
82+
if ctx.Error != nil {
83+
return ctx.Error
84+
}
85+
err = ctx.PrintUsage(false)
86+
if err != nil {
87+
return err
88+
}
89+
fmt.Fprintln(realCtx.Stdout)
90+
return nil
91+
}

cmd/gentoken/main.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"time"
7+
8+
_ "github.com/alecthomas/kong"
9+
jwt "github.com/golang-jwt/jwt/v4"
10+
"github.com/spf13/pflag"
11+
)
12+
13+
var (
14+
cliAlgorithm = pflag.StringP(
15+
"algorithm",
16+
"",
17+
"HS256",
18+
"Signing algorithm - possible values are HS256, HS384, HS512",
19+
)
20+
cliTimeout = pflag.DurationP("timeout", "", 2*time.Hour, "JWT token expires time")
21+
help = pflag.BoolP("help", "h", false, "Print this help message")
22+
)
23+
24+
func main() {
25+
pflag.Usage = func() {
26+
fmt.Println(`Usage: gentoken [OPTIONS] SECRETID SECRETKEY`)
27+
pflag.PrintDefaults()
28+
}
29+
pflag.Parse()
30+
31+
if *help {
32+
pflag.Usage()
33+
34+
return
35+
}
36+
37+
if pflag.NArg() != 2 {
38+
pflag.Usage()
39+
os.Exit(1)
40+
}
41+
42+
token, err := createJWTToken(*cliAlgorithm, *cliTimeout, os.Args[1], os.Args[2])
43+
if err != nil {
44+
fmt.Printf("Error: %s\n", err.Error())
45+
46+
return
47+
}
48+
49+
fmt.Println(token)
50+
}
51+
52+
func createJWTToken(algorithm string, timeout time.Duration, secretID, secretKey string) (string, error) {
53+
expire := time.Now().Add(timeout)
54+
55+
token := jwt.NewWithClaims(jwt.GetSigningMethod(algorithm), jwt.MapClaims{
56+
"kid": secretID,
57+
"exp": expire.Unix(),
58+
"iat": time.Now().Unix(),
59+
})
60+
61+
return token.SignedString([]byte(secretKey))
62+
}

cmd/protofmt/main.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"flag"
6+
"fmt"
7+
"io"
8+
"log"
9+
"os"
10+
"path/filepath"
11+
"regexp"
12+
"strings"
13+
"text/tabwriter"
14+
)
15+
16+
func main() {
17+
flag.Parse()
18+
for _, arg := range flag.Args() {
19+
matches, err := filepath.Glob(arg)
20+
if err != nil {
21+
log.Fatal(err)
22+
}
23+
for _, file := range matches {
24+
if stat, err := os.Stat(file); err != nil {
25+
log.Fatal(err)
26+
} else if stat.IsDir() {
27+
err := filepath.Walk(file, func(path string, info os.FileInfo, err error) error {
28+
if err != nil {
29+
return err
30+
}
31+
if filepath.Ext(path) == ".proto" {
32+
return formatProtoFile(path)
33+
}
34+
return nil
35+
})
36+
if err != nil {
37+
log.Fatal(err)
38+
}
39+
} else {
40+
if err := formatProtoFile(file); err != nil {
41+
log.Fatal(err)
42+
}
43+
}
44+
}
45+
}
46+
}
47+
48+
func formatProtoFile(file string) error {
49+
log.Println("Formatting", file)
50+
in, err := os.Open(file)
51+
if err != nil {
52+
return err
53+
}
54+
defer in.Close()
55+
out, err := os.Create(file + ".tmp")
56+
if err != nil {
57+
return err
58+
}
59+
defer out.Close()
60+
if err := formatProto(in, out); err != nil {
61+
return err
62+
}
63+
in.Close()
64+
out.Close()
65+
return os.Rename(file+".tmp", file)
66+
}
67+
68+
func formatProto(in io.Reader, out io.Writer) error {
69+
sc := bufio.NewScanner(in)
70+
lineExp := regexp.MustCompile(`([^=]+)\s+([^=\s]+?)\s*=(.+)`)
71+
var tw *tabwriter.Writer
72+
for sc.Scan() {
73+
line := sc.Text()
74+
if strings.HasPrefix(line, "//") {
75+
if _, err := fmt.Fprintln(out, line); err != nil {
76+
return err
77+
}
78+
continue
79+
}
80+
81+
ms := lineExp.FindStringSubmatch(line)
82+
for i := range ms {
83+
ms[i] = strings.TrimSpace(ms[i])
84+
}
85+
if len(ms) == 4 && ms[1] != "option" {
86+
typ := strings.Join(strings.Fields(ms[1]), " ")
87+
name := ms[2]
88+
id := ms[3]
89+
if tw == nil {
90+
tw = tabwriter.NewWriter(out, 4, 4, 1, ' ', 0)
91+
}
92+
if typ == "" {
93+
// We're in an enum
94+
fmt.Fprintf(tw, "\t%s\t= %s\n", name, id)
95+
} else {
96+
// Message
97+
fmt.Fprintf(tw, "\t%s\t%s\t= %s\n", typ, name, id)
98+
}
99+
} else {
100+
if tw != nil {
101+
if err := tw.Flush(); err != nil {
102+
return err
103+
}
104+
tw = nil
105+
}
106+
if _, err := fmt.Fprintln(out, line); err != nil {
107+
return err
108+
}
109+
}
110+
}
111+
112+
return nil
113+
}

cryptoutil/_doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ package cryptoutil
33
// https://github.com/bitnami-labs/sealed-secrets/tree/main/pkg/crypto
44
// https://github.com/smallstep/crypto
55
// https://github.com/simia-tech/crypt
6+
// https://github.com/golang/crypto

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ module github.com/pubgo/funk
33
go 1.18
44

55
require (
6+
github.com/alecthomas/kong v0.6.1
67
github.com/fatih/color v1.13.0
78
github.com/go-kit/log v0.2.1
89
github.com/go-logr/logr v1.2.2
10+
github.com/golang-jwt/jwt/v4 v4.4.2
911
github.com/gopherjs/gopherjs v1.17.2
1012
github.com/iancoleman/strcase v0.2.0
1113
github.com/iand/logfmtr v0.2.1
14+
github.com/mattn/go-isatty v0.0.14
1215
github.com/modood/table v0.0.0-20220527013332-8d47e76dad33
1316
github.com/rodaine/table v1.0.1
17+
github.com/spf13/pflag v1.0.5
1418
github.com/stretchr/testify v1.7.2
1519
github.com/valyala/fastrand v1.1.0
1620
go.uber.org/atomic v1.9.0
@@ -22,7 +26,6 @@ require (
2226
github.com/go-logfmt/logfmt v0.5.1 // indirect
2327
github.com/kr/pretty v0.1.0 // indirect
2428
github.com/mattn/go-colorable v0.1.9 // indirect
25-
github.com/mattn/go-isatty v0.0.14 // indirect
2629
github.com/pmezard/go-difflib v1.0.0 // indirect
2730
github.com/smartystreets/goconvey v1.7.2 // indirect
2831
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
github.com/alecthomas/kong v0.6.1 h1:1kNhcFepkR+HmasQpbiKDLylIL8yh5B5y1zPp5bJimA=
2+
github.com/alecthomas/kong v0.6.1/go.mod h1:JfHWDzLmbh/puW6I3V7uWenoh56YNVONW+w8eKeUr9I=
3+
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48=
4+
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
15
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
26
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
37
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -10,6 +14,8 @@ github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KE
1014
github.com/go-logr/logr v1.1.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
1115
github.com/go-logr/logr v1.2.2 h1:ahHml/yUpnlb96Rp8HCvtYVPY8ZYpxq3g7UYchIYwbs=
1216
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
17+
github.com/golang-jwt/jwt/v4 v4.4.2 h1:rcc4lwaZgFMCZ5jxF9ABolDcIHdBytAFgqFPbSJQAYs=
18+
github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
1319
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
1420
github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g=
1521
github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k=
@@ -41,6 +47,8 @@ github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N
4147
github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
4248
github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg=
4349
github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM=
50+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
51+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
4452
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4553
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
4654
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=

internal/notify/_doc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package notify
2+
3+
// https://github.com/nikoksr/notify

proto/errors.proto

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
syntax = "proto3";
2+
3+
import "google/protobuf/descriptor.proto";
4+
5+
package errors;
6+
7+
option cc_enable_arenas = true;
8+
option java_multiple_files = true;
9+
option java_package = "com.lava.errors";
10+
option go_package = "github.com/pubgo/lava/errors;errors";
11+
12+
message Error {
13+
int32 status = 1;
14+
string type = 2;
15+
string title = 3;
16+
string reason = 4;
17+
string detail = 5;
18+
string instance = 6;
19+
map<string, string> tags = 7;
20+
}
21+
22+
extend google.protobuf.EnumValueOptions {
23+
int32 code = 100000;
24+
}

proto/event.proto

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
syntax = "proto3";
2+
3+
package event;
4+
5+
option go_package = "github.com/pubgo/lava/event;event";
6+
7+
enum EventType {
8+
UNKNOWN = 0;
9+
CREATE = 1;
10+
UPDATE = 2;
11+
DELETE = 3;
12+
}

termutil/term.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package termutil
2+
3+
import (
4+
_ "github.com/mattn/go-isatty"
5+
)

0 commit comments

Comments
 (0)