-
Notifications
You must be signed in to change notification settings - Fork 9
/
newid.go
47 lines (38 loc) · 1004 Bytes
/
newid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package source
import (
"context"
"fmt"
"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/go-ids"
)
var (
n = 12
base62 = false
)
func newNewIdCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "new-id",
Short: "Generate a new ID.",
Args: cobra.NoArgs,
RunE: fncobra.RunE(func(ctx context.Context, args []string) error {
stdout := console.Stdout(ctx)
fmt.Fprintln(stdout, newId())
return nil
}),
}
cmd.Flags().IntVar(&n, "len", n, "Number of bytes.")
cmd.Flags().BoolVar(&base62, "base62", base62, "Generate a base62-based random ID.")
return cmd
}
func newId() string {
if base62 {
return ids.NewRandomBase62ID(n)
} else {
return ids.NewRandomBase32ID(n)
}
}