Skip to content

Commit

Permalink
move generation of request id to common.go
Browse files Browse the repository at this point in the history
  • Loading branch information
JadeRedworth committed Aug 17, 2018
1 parent 70f44cc commit d4b4fa7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
14 changes: 14 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"log"
"os"
"os/exec"
"encoding/base32"
"os/signal"
"path/filepath"
"crypto/rand"
"strings"
"time"
"unicode"
Expand All @@ -32,6 +34,8 @@ const (
FunctionsDockerImage = "fnproject/fnserver"
FuncfileDockerRuntime = "docker"
MinRequiredDockerVersion = "17.5.0"
RequestID = "request-id"
RequestedLength = 16
)

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

func GetRequestID() string {
byteArr := make([]byte, RequestedLength)
_, err := rand.Read(byteArr)
if err != nil {
log.Fatalf("failed to generate random number for requestID")
}

return base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(byteArr)
}

// BuildFunc bumps version and builds function.
func BuildFunc(c *cli.Context, fpath string, funcfile *FuncFile, buildArg []string, noCache bool) (*FuncFile, error) {
var err error
Expand Down
16 changes: 2 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,22 @@ package main

import (
"bytes"
"crypto/rand"
"encoding/base32"
"fmt"
"io"
"log"
"os"
"sort"
"strings"
"text/tabwriter"
"text/template"

"github.com/fnproject/cli/commands"
"github.com/fnproject/cli/common"
"github.com/fnproject/cli/common/color"
"github.com/fnproject/cli/config"
"github.com/spf13/viper"
"github.com/urfave/cli"
)

func getRequestID() string {
byteArr := make([]byte, 16)
_, err := rand.Read(byteArr)
if err != nil {
log.Fatalf("failed to generate random number for requestID")
}

return base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(byteArr)
}

func newFn() *cli.App {
app := cli.NewApp()
app.Name = "fn"
Expand All @@ -43,7 +31,7 @@ func newFn() *cli.App {
return err
}

viper.Set("request-id", getRequestID())
viper.Set(common.RequestID, common.GetRequestID())
commandArgOverrides(c)
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion objects/app/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"github.com/fnproject/cli/client"
"github.com/fnproject/cli/common"
fnclient "github.com/fnproject/fn_go/client"
fnclient "github.com/fnproject/fn_go/client"
apiapps "github.com/fnproject/fn_go/client/apps"
"github.com/fnproject/fn_go/models"
"github.com/fnproject/fn_go/provider"
Expand Down

0 comments on commit d4b4fa7

Please sign in to comment.