Skip to content

Commit d4b4fa7

Browse files
committed
move generation of request id to common.go
1 parent 70f44cc commit d4b4fa7

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

common/common.go

+14
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import (
1111
"log"
1212
"os"
1313
"os/exec"
14+
"encoding/base32"
1415
"os/signal"
1516
"path/filepath"
17+
"crypto/rand"
1618
"strings"
1719
"time"
1820
"unicode"
@@ -32,6 +34,8 @@ const (
3234
FunctionsDockerImage = "fnproject/fnserver"
3335
FuncfileDockerRuntime = "docker"
3436
MinRequiredDockerVersion = "17.5.0"
37+
RequestID = "request-id"
38+
RequestedLength = 16
3539
)
3640

3741
// GetWd returns working directory.
@@ -55,6 +59,16 @@ func GetDir(c *cli.Context) string {
5559
return dir
5660
}
5761

62+
func GetRequestID() string {
63+
byteArr := make([]byte, RequestedLength)
64+
_, err := rand.Read(byteArr)
65+
if err != nil {
66+
log.Fatalf("failed to generate random number for requestID")
67+
}
68+
69+
return base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(byteArr)
70+
}
71+
5872
// BuildFunc bumps version and builds function.
5973
func BuildFunc(c *cli.Context, fpath string, funcfile *FuncFile, buildArg []string, noCache bool) (*FuncFile, error) {
6074
var err error

main.go

+2-14
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,22 @@ package main
22

33
import (
44
"bytes"
5-
"crypto/rand"
6-
"encoding/base32"
75
"fmt"
86
"io"
9-
"log"
107
"os"
118
"sort"
129
"strings"
1310
"text/tabwriter"
1411
"text/template"
1512

1613
"github.com/fnproject/cli/commands"
14+
"github.com/fnproject/cli/common"
1715
"github.com/fnproject/cli/common/color"
1816
"github.com/fnproject/cli/config"
1917
"github.com/spf13/viper"
2018
"github.com/urfave/cli"
2119
)
2220

23-
func getRequestID() string {
24-
byteArr := make([]byte, 16)
25-
_, err := rand.Read(byteArr)
26-
if err != nil {
27-
log.Fatalf("failed to generate random number for requestID")
28-
}
29-
30-
return base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(byteArr)
31-
}
32-
3321
func newFn() *cli.App {
3422
app := cli.NewApp()
3523
app.Name = "fn"
@@ -43,7 +31,7 @@ func newFn() *cli.App {
4331
return err
4432
}
4533

46-
viper.Set("request-id", getRequestID())
34+
viper.Set(common.RequestID, common.GetRequestID())
4735
commandArgOverrides(c)
4836
return nil
4937
}

objects/app/apps.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
"github.com/fnproject/cli/client"
1414
"github.com/fnproject/cli/common"
15-
fnclient "github.com/fnproject/fn_go/client"
15+
fnclient "github.com/fnproject/fn_go/client"
1616
apiapps "github.com/fnproject/fn_go/client/apps"
1717
"github.com/fnproject/fn_go/models"
1818
"github.com/fnproject/fn_go/provider"

0 commit comments

Comments
 (0)