vvauth 2.4.0 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish-npm.yml +5 -0
- package/.github/workflows/release.yml +33 -0
- package/.github/workflows/test.yml +38 -0
- package/.goreleaser.yaml +31 -0
- package/README.md +17 -0
- package/cache.go +101 -0
- package/cmd/vauth/main.go +202 -0
- package/go.mod +16 -0
- package/go.sum +14 -0
- package/humandiff.go +50 -0
- package/index.js +1 -1
- package/package.json +1 -1
- package/rc.go +427 -0
- package/test/token-cache.js +24 -0
- package/vault.go +238 -0
- package/vauth.go +518 -0
- package/vauth_test.go +664 -0
|
@@ -17,7 +17,12 @@ jobs:
|
|
|
17
17
|
with:
|
|
18
18
|
node-version: '24.x'
|
|
19
19
|
registry-url: 'https://registry.npmjs.org'
|
|
20
|
+
- uses: actions/setup-go@v5
|
|
21
|
+
with:
|
|
22
|
+
go-version-file: go.mod
|
|
20
23
|
- run: npm install
|
|
21
24
|
- run: npm run eslint
|
|
22
25
|
- run: npm test
|
|
26
|
+
- run: go vet ./...
|
|
27
|
+
- run: go test ./...
|
|
23
28
|
- run: npm publish
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
name: release
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
tags:
|
|
5
|
+
- 'v*'
|
|
6
|
+
|
|
7
|
+
permissions:
|
|
8
|
+
contents: write
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
goreleaser:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
environment: production
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v4
|
|
16
|
+
with:
|
|
17
|
+
fetch-depth: 0
|
|
18
|
+
- uses: actions/setup-go@v5
|
|
19
|
+
with:
|
|
20
|
+
go-version-file: go.mod
|
|
21
|
+
- run: go vet ./... && go test ./...
|
|
22
|
+
- uses: actions/checkout@v4
|
|
23
|
+
with:
|
|
24
|
+
repository: 131/gh-actions
|
|
25
|
+
ref: v1
|
|
26
|
+
path: .gh-actions
|
|
27
|
+
- uses: goreleaser/goreleaser-action@v6
|
|
28
|
+
with:
|
|
29
|
+
version: '~> v2'
|
|
30
|
+
args: release --clean
|
|
31
|
+
env:
|
|
32
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
33
|
+
SIGNING_SERVER: ${{ secrets.SIGNING_SERVER }}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
name: test
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
branches: ['**']
|
|
5
|
+
pull_request:
|
|
6
|
+
|
|
7
|
+
permissions:
|
|
8
|
+
contents: read
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
node:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v4
|
|
15
|
+
- uses: actions/setup-node@v4
|
|
16
|
+
with:
|
|
17
|
+
node-version: '24.x'
|
|
18
|
+
- run: npm install
|
|
19
|
+
- run: npm run eslint
|
|
20
|
+
- run: npm test
|
|
21
|
+
|
|
22
|
+
go:
|
|
23
|
+
strategy:
|
|
24
|
+
matrix:
|
|
25
|
+
os: [ubuntu-latest, windows-latest]
|
|
26
|
+
runs-on: ${{ matrix.os }}
|
|
27
|
+
steps:
|
|
28
|
+
- uses: actions/checkout@v4
|
|
29
|
+
- uses: actions/setup-go@v5
|
|
30
|
+
with:
|
|
31
|
+
go-version-file: go.mod
|
|
32
|
+
- run: gofmt -l . && test -z "$(gofmt -l .)"
|
|
33
|
+
shell: bash
|
|
34
|
+
- run: go vet ./...
|
|
35
|
+
- run: go test ./...
|
|
36
|
+
if: runner.os == 'Linux'
|
|
37
|
+
- run: go build ./cmd/vauth
|
|
38
|
+
if: runner.os == 'Windows'
|
package/.goreleaser.yaml
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
version: 2
|
|
2
|
+
|
|
3
|
+
project_name: vauth
|
|
4
|
+
|
|
5
|
+
builds:
|
|
6
|
+
- id: linux
|
|
7
|
+
main: ./cmd/vauth
|
|
8
|
+
goos: [linux]
|
|
9
|
+
goarch: [amd64]
|
|
10
|
+
flags: [-trimpath]
|
|
11
|
+
ldflags: ['-s -w -X main.version={{ .Tag }}']
|
|
12
|
+
- id: windows
|
|
13
|
+
main: ./cmd/vauth
|
|
14
|
+
goos: [windows]
|
|
15
|
+
goarch: [amd64]
|
|
16
|
+
flags: [-trimpath]
|
|
17
|
+
ldflags: ['-s -w -X main.version={{ .Tag }}']
|
|
18
|
+
hooks:
|
|
19
|
+
post: .gh-actions/sign-binaries/sign {{ .Path }}
|
|
20
|
+
|
|
21
|
+
archives:
|
|
22
|
+
- formats: [binary]
|
|
23
|
+
name_template: '{{ .ProjectName }}-{{ .Os }}-{{ .Arch }}'
|
|
24
|
+
|
|
25
|
+
checksum:
|
|
26
|
+
name_template: SHA256SUMS
|
|
27
|
+
|
|
28
|
+
changelog:
|
|
29
|
+
filters:
|
|
30
|
+
exclude:
|
|
31
|
+
- '^v[0-9]+\.[0-9]+\.[0-9]+$'
|
package/README.md
CHANGED
|
@@ -46,6 +46,23 @@ env:
|
|
|
46
46
|
```
|
|
47
47
|
|
|
48
48
|
|
|
49
|
+
# Installation
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
npm install -g vvauth # node
|
|
53
|
+
go install github.com/131/vvauth/v2/cmd/vauth@latest # go
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Both ship the same `vauth` command, flags and `.vauthrc`, and share the token
|
|
57
|
+
cache. The Go binary has no runtime dependency; signed `vauth-windows-amd64.exe`
|
|
58
|
+
and `vauth-linux-amd64` builds are attached to each
|
|
59
|
+
[release](https://github.com/131/vvauth/releases). `vauth version` prints the
|
|
60
|
+
build version.
|
|
61
|
+
|
|
62
|
+
On Windows, the SSH agent is `SSH_AUTH_SOCK` when set, else Pageant (≥ 0.75,
|
|
63
|
+
named pipe autodiscovery), else the OpenSSH agent pipe.
|
|
64
|
+
|
|
65
|
+
|
|
49
66
|
# Credits
|
|
50
67
|
* [Francois Leurent](https://github.com/131)
|
|
51
68
|
|
package/cache.go
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
package vvauth
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"crypto/sha256"
|
|
5
|
+
"encoding/hex"
|
|
6
|
+
"encoding/json"
|
|
7
|
+
"fmt"
|
|
8
|
+
"os"
|
|
9
|
+
"path/filepath"
|
|
10
|
+
"strings"
|
|
11
|
+
|
|
12
|
+
sshagentcrypt "github.com/131/ssh-agent-crypt"
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
// tokenCache is the clear content of ~/.vauth/tokens/<key>.creds.
|
|
16
|
+
type tokenCache struct {
|
|
17
|
+
Token string `json:"token"`
|
|
18
|
+
LeaseDuration int64 `json:"lease_duration"`
|
|
19
|
+
Renewable bool `json:"renewable"`
|
|
20
|
+
ExpiresAt *int64 `json:"expires_at"` // unix milliseconds, null when unlimited
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// TokenCacheKey isolates cached tokens by Vault address and authentication
|
|
24
|
+
// configuration. It matches the Node implementation byte for byte.
|
|
25
|
+
func (v *Vauth) TokenCacheKey() string {
|
|
26
|
+
key := `{"vault_addr":` + string(jsonString(strings.Trim(v.VaultAddr, "/")))
|
|
27
|
+
if auth := v.RC.AuthKey(); auth != "" {
|
|
28
|
+
key += "," + auth
|
|
29
|
+
}
|
|
30
|
+
digest := sha256.Sum256([]byte(key + "}"))
|
|
31
|
+
return hex.EncodeToString(digest[:])
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// TokenCacheFile is the armored cache file for this configuration.
|
|
35
|
+
func (v *Vauth) TokenCacheFile() string {
|
|
36
|
+
return filepath.Join(v.CacheDir, v.TokenCacheKey()+".creds")
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
func (v *Vauth) agentOptions() *sshagentcrypt.Options {
|
|
40
|
+
return &sshagentcrypt.Options{AgentSocket: v.Agent}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
func (v *Vauth) readTokenCache() *tokenCache {
|
|
44
|
+
if !v.TokenCache {
|
|
45
|
+
return nil
|
|
46
|
+
}
|
|
47
|
+
armor, err := os.ReadFile(v.TokenCacheFile())
|
|
48
|
+
if err != nil {
|
|
49
|
+
return nil
|
|
50
|
+
}
|
|
51
|
+
body, err := sshagentcrypt.Decrypt(armor, "", v.agentOptions())
|
|
52
|
+
if err != nil {
|
|
53
|
+
return nil
|
|
54
|
+
}
|
|
55
|
+
var cache tokenCache
|
|
56
|
+
if json.Unmarshal(body, &cache) != nil || cache.Token == "" {
|
|
57
|
+
return nil
|
|
58
|
+
}
|
|
59
|
+
return &cache
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
func (v *Vauth) saveTokenCache(token *token) {
|
|
63
|
+
if !v.TokenCache {
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
file := v.TokenCacheFile()
|
|
67
|
+
temporary := file + ".next"
|
|
68
|
+
defer os.Remove(temporary)
|
|
69
|
+
cache := tokenCache{Token: token.Value, LeaseDuration: token.LeaseDuration, Renewable: token.Renewable}
|
|
70
|
+
if token.TTL != 0 {
|
|
71
|
+
expires := v.now().UnixMilli() + int64(token.TTL*1000)
|
|
72
|
+
cache.ExpiresAt = &expires
|
|
73
|
+
}
|
|
74
|
+
err := v.writeTokenCache(file, temporary, &cache)
|
|
75
|
+
if err != nil {
|
|
76
|
+
// Cache failures must not prevent authentication or expose credentials.
|
|
77
|
+
fmt.Fprintln(v.stderr(), "Could not encrypt/write token cache; token not persisted")
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
func (v *Vauth) writeTokenCache(file, temporary string, cache *tokenCache) error {
|
|
82
|
+
body, err := json.Marshal(cache)
|
|
83
|
+
if err != nil {
|
|
84
|
+
return err
|
|
85
|
+
}
|
|
86
|
+
if err := os.MkdirAll(filepath.Dir(file), 0o700); err != nil {
|
|
87
|
+
return err
|
|
88
|
+
}
|
|
89
|
+
identity := v.Environ["VAUTH_USER_IDENTITY"]
|
|
90
|
+
if identity == "" {
|
|
91
|
+
identity = v.Environ["VAUTH_USER_MAIL"]
|
|
92
|
+
}
|
|
93
|
+
armor, err := sshagentcrypt.Encrypt(body, identity, v.agentOptions())
|
|
94
|
+
if err != nil {
|
|
95
|
+
return err
|
|
96
|
+
}
|
|
97
|
+
if err := os.WriteFile(temporary, []byte(armor), 0o600); err != nil {
|
|
98
|
+
return err
|
|
99
|
+
}
|
|
100
|
+
return os.Rename(temporary, file)
|
|
101
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// Command vauth is the standalone Vault credentials helper: same commands
|
|
2
|
+
// and flags as the vvauth npm package (cnyks runner).
|
|
3
|
+
package main
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"encoding/json"
|
|
7
|
+
"fmt"
|
|
8
|
+
"os"
|
|
9
|
+
"regexp"
|
|
10
|
+
"runtime/debug"
|
|
11
|
+
"strings"
|
|
12
|
+
|
|
13
|
+
vvauth "github.com/131/vvauth/v2"
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
const usage = `Usage: vauth <command> [--flag[=value]] [--ir://json]
|
|
17
|
+
|
|
18
|
+
Commands:
|
|
19
|
+
env [--source] [--renew] Resolve the .vauthrc environment (JSON with --ir://json)
|
|
20
|
+
login [--source] [--renew] Authenticate; --source exports VAULT_TOKEN
|
|
21
|
+
connect [--renew] Authenticate and print the token
|
|
22
|
+
run <command> [args...] Run a command with the resolved environment
|
|
23
|
+
dotenv Print the environment as K=v lines
|
|
24
|
+
show Print the identity profile and .vauth_database
|
|
25
|
+
set <key> <value> Store a key in the .vauth_database
|
|
26
|
+
unset <key> Remove a key from the .vauth_database
|
|
27
|
+
install Install the venv shell function in ~/.bashrc
|
|
28
|
+
version Print the build version
|
|
29
|
+
`
|
|
30
|
+
|
|
31
|
+
// version is set by the release build (-ldflags "-X main.version=v1.2.3");
|
|
32
|
+
// otherwise the module version recorded by go install is used.
|
|
33
|
+
var version string
|
|
34
|
+
|
|
35
|
+
func buildVersion() string {
|
|
36
|
+
if version != "" {
|
|
37
|
+
return version
|
|
38
|
+
}
|
|
39
|
+
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "" {
|
|
40
|
+
return info.Main.Version
|
|
41
|
+
}
|
|
42
|
+
return "(devel)"
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
var flagPattern = regexp.MustCompile(`^--?([a-zA-Z_0-9/:-]+)(?:=(.*))?$`)
|
|
46
|
+
|
|
47
|
+
// parseArgs splits positional arguments from --flags (nyks/process/parseArgs).
|
|
48
|
+
func parseArgs(argv []string) ([]string, map[string]string) {
|
|
49
|
+
var args []string
|
|
50
|
+
dict := map[string]string{}
|
|
51
|
+
for i, arg := range argv {
|
|
52
|
+
if arg == "--" {
|
|
53
|
+
args = append(args, argv[i+1:]...)
|
|
54
|
+
break
|
|
55
|
+
}
|
|
56
|
+
if !strings.HasPrefix(arg, "-") {
|
|
57
|
+
args = append(args, arg)
|
|
58
|
+
continue
|
|
59
|
+
}
|
|
60
|
+
if match := flagPattern.FindStringSubmatch(arg); match != nil {
|
|
61
|
+
value := "true"
|
|
62
|
+
if strings.Contains(arg, "=") {
|
|
63
|
+
value = match[2]
|
|
64
|
+
}
|
|
65
|
+
dict[match[1]] = value
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return args, dict
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
func truthy(value string, ok bool) bool {
|
|
72
|
+
if !ok {
|
|
73
|
+
return false
|
|
74
|
+
}
|
|
75
|
+
switch strings.ToLower(value) {
|
|
76
|
+
case "", "false", "0":
|
|
77
|
+
return false
|
|
78
|
+
}
|
|
79
|
+
return true
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// param reads the nth positional argument or the --name flag.
|
|
83
|
+
func param(args []string, dict map[string]string, index int, name string) (string, bool) {
|
|
84
|
+
if index < len(args) {
|
|
85
|
+
return args[index], true
|
|
86
|
+
}
|
|
87
|
+
value, ok := dict[name]
|
|
88
|
+
return value, ok
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
func main() {
|
|
92
|
+
if err := run(os.Args[1:]); err != nil {
|
|
93
|
+
fmt.Fprintln(os.Stderr, err)
|
|
94
|
+
os.Exit(1)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
func run(argv []string) error {
|
|
99
|
+
if len(argv) > 0 && argv[0] == "run" {
|
|
100
|
+
return runCommand(argv[1:])
|
|
101
|
+
}
|
|
102
|
+
args, dict := parseArgs(argv)
|
|
103
|
+
if _, ok := dict["version"]; ok && len(args) == 0 {
|
|
104
|
+
args = []string{"version"}
|
|
105
|
+
}
|
|
106
|
+
if len(args) == 0 {
|
|
107
|
+
fmt.Fprint(os.Stderr, usage)
|
|
108
|
+
os.Exit(1)
|
|
109
|
+
}
|
|
110
|
+
command, args := args[0], args[1:]
|
|
111
|
+
_, jsonOutput := dict["ir://json"]
|
|
112
|
+
if command == "version" || command == "--version" {
|
|
113
|
+
fmt.Println(buildVersion())
|
|
114
|
+
return nil
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
v, err := vvauth.New()
|
|
118
|
+
if err != nil {
|
|
119
|
+
return err
|
|
120
|
+
}
|
|
121
|
+
switch command {
|
|
122
|
+
case "env":
|
|
123
|
+
source, renew := truthy(param(args, dict, 0, "source")), truthy(param(args, dict, 1, "renew"))
|
|
124
|
+
env, err := v.Env(renew)
|
|
125
|
+
if err != nil {
|
|
126
|
+
return err
|
|
127
|
+
}
|
|
128
|
+
if source {
|
|
129
|
+
v.PublishEnv(env)
|
|
130
|
+
return nil
|
|
131
|
+
}
|
|
132
|
+
return respond(env, jsonOutput)
|
|
133
|
+
case "login":
|
|
134
|
+
source, renew := truthy(param(args, dict, 0, "source")), truthy(param(args, dict, 1, "renew"))
|
|
135
|
+
return v.Login(source, renew)
|
|
136
|
+
case "connect":
|
|
137
|
+
token, err := v.Connect(truthy(param(args, dict, 0, "renew")))
|
|
138
|
+
if err != nil {
|
|
139
|
+
return err
|
|
140
|
+
}
|
|
141
|
+
if token == "" {
|
|
142
|
+
return nil
|
|
143
|
+
}
|
|
144
|
+
return respond(token, jsonOutput)
|
|
145
|
+
case "dotenv":
|
|
146
|
+
return v.Dotenv()
|
|
147
|
+
case "show":
|
|
148
|
+
profile, err := v.Show()
|
|
149
|
+
if err != nil {
|
|
150
|
+
return err
|
|
151
|
+
}
|
|
152
|
+
return respond(profile, jsonOutput)
|
|
153
|
+
case "set":
|
|
154
|
+
key, ok := param(args, dict, 0, "k")
|
|
155
|
+
if !ok {
|
|
156
|
+
return fmt.Errorf("Missing parameter --k")
|
|
157
|
+
}
|
|
158
|
+
value, ok := param(args, dict, 1, "v")
|
|
159
|
+
if !ok {
|
|
160
|
+
return fmt.Errorf("Missing parameter --v")
|
|
161
|
+
}
|
|
162
|
+
return v.Set(key, value)
|
|
163
|
+
case "unset":
|
|
164
|
+
key, ok := param(args, dict, 0, "k")
|
|
165
|
+
if !ok {
|
|
166
|
+
return fmt.Errorf("Missing parameter --k")
|
|
167
|
+
}
|
|
168
|
+
return v.Unset(key)
|
|
169
|
+
case "install":
|
|
170
|
+
return v.Install()
|
|
171
|
+
}
|
|
172
|
+
return fmt.Errorf("Invalid command key '%s'", command)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
func runCommand(argv []string) error {
|
|
176
|
+
if len(argv) == 0 {
|
|
177
|
+
return fmt.Errorf("run: missing command")
|
|
178
|
+
}
|
|
179
|
+
v, err := vvauth.New()
|
|
180
|
+
if err != nil {
|
|
181
|
+
return err
|
|
182
|
+
}
|
|
183
|
+
code, err := v.Run(argv[0], argv[1:]...)
|
|
184
|
+
if err != nil {
|
|
185
|
+
return err
|
|
186
|
+
}
|
|
187
|
+
os.Exit(code)
|
|
188
|
+
return nil
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// respond prints the command result: JSON with --ir://json, raw strings
|
|
192
|
+
// otherwise (objects are still rendered as JSON).
|
|
193
|
+
func respond(response any, jsonOutput bool) error {
|
|
194
|
+
if text, ok := response.(string); ok && !jsonOutput {
|
|
195
|
+
// cnyks --ir://raw writes String(response) without a trailing newline.
|
|
196
|
+
_, err := fmt.Fprint(os.Stdout, text)
|
|
197
|
+
return err
|
|
198
|
+
}
|
|
199
|
+
encoder := json.NewEncoder(os.Stdout)
|
|
200
|
+
encoder.SetEscapeHTML(false)
|
|
201
|
+
return encoder.Encode(response)
|
|
202
|
+
}
|
package/go.mod
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module github.com/131/vvauth/v2
|
|
2
|
+
|
|
3
|
+
// version: 2.5.0
|
|
4
|
+
|
|
5
|
+
go 1.22
|
|
6
|
+
|
|
7
|
+
require (
|
|
8
|
+
github.com/131/ssh-agent-crypt v1.3.3
|
|
9
|
+
golang.org/x/crypto v0.31.0
|
|
10
|
+
gopkg.in/yaml.v3 v3.0.1
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
require (
|
|
14
|
+
github.com/Microsoft/go-winio v0.6.2 // indirect
|
|
15
|
+
golang.org/x/sys v0.28.0 // indirect
|
|
16
|
+
)
|
package/go.sum
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
github.com/131/ssh-agent-crypt v1.3.3 h1:+sITI4C55Ck5Kype/aKcn/6MoGeCfk9GshogF0LZgkI=
|
|
2
|
+
github.com/131/ssh-agent-crypt v1.3.3/go.mod h1:XNiI2n3+2NTwjhyd/plFhrd0vSfWOZ58ocvqTlx1Ydk=
|
|
3
|
+
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
|
|
4
|
+
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
|
|
5
|
+
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
|
|
6
|
+
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
|
7
|
+
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
|
|
8
|
+
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
|
9
|
+
golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
|
|
10
|
+
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
|
|
11
|
+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
|
12
|
+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
13
|
+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
|
14
|
+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
package/humandiff.go
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
package vvauth
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"fmt"
|
|
5
|
+
"math"
|
|
6
|
+
"strings"
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
type diffStep struct {
|
|
10
|
+
format string
|
|
11
|
+
size float64
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// nyks/date/humanDiff FORMAT_LIGHT steps.
|
|
15
|
+
var diffSteps = []diffStep{
|
|
16
|
+
{"%ds", 60}, {"%dm", 60}, {"%dh", 24}, {"%dd", 30}, {"%dmonth%s", 12}, {"%dy", 0},
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// HumanDiff formats a duration in seconds with at most max units ("1d 3h"),
|
|
20
|
+
// like nyks/date/humanDiff.
|
|
21
|
+
func HumanDiff(seconds float64, max int) string {
|
|
22
|
+
seconds = math.Ceil(seconds)
|
|
23
|
+
if seconds == 0 {
|
|
24
|
+
return "0s"
|
|
25
|
+
}
|
|
26
|
+
var parts []string
|
|
27
|
+
for _, step := range diffSteps {
|
|
28
|
+
var current float64
|
|
29
|
+
if step.size == 0 {
|
|
30
|
+
current = math.Floor(seconds)
|
|
31
|
+
} else {
|
|
32
|
+
current = math.Floor(math.Mod(seconds, step.size))
|
|
33
|
+
seconds /= step.size
|
|
34
|
+
}
|
|
35
|
+
if current > 0 {
|
|
36
|
+
plural := ""
|
|
37
|
+
if current > 1 {
|
|
38
|
+
plural = "s"
|
|
39
|
+
}
|
|
40
|
+
parts = append(parts, fmt.Sprintf(strings.Replace(step.format, "%s", plural, 1), int64(current)))
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
for i, j := 0, len(parts)-1; i < j; i, j = i+1, j-1 {
|
|
44
|
+
parts[i], parts[j] = parts[j], parts[i]
|
|
45
|
+
}
|
|
46
|
+
if len(parts) > max {
|
|
47
|
+
parts = parts[:max]
|
|
48
|
+
}
|
|
49
|
+
return strings.Join(parts, " ")
|
|
50
|
+
}
|
package/index.js
CHANGED