Skip to content

Commit

Permalink
Pass build info using ldflags
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles-Antoine Mathieu committed Sep 29, 2020
1 parent c53f989 commit 156baf9
Show file tree
Hide file tree
Showing 16 changed files with 287 additions and 176 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ documentation
release
releases
server/plikd
server/common/version.go
testing
webapp/node_modules
webapp/bower_components
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ webapp/dist
clients
client/client
servers
server/common/version.go
release
releases
debs
Expand Down
34 changes: 17 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ GOHOSTARCH=$(shell go env GOHOSTARCH)
DEBROOT_SERVER=debs/server
DEBROOT_CLIENT=debs/client

race_detector = GORACE="halt_on_error=1" go build -race
BUILD_INFO=$(shell server/gen_build_info.sh $(RELEASE_VERSION) base64)
BUILD_FLAG=-ldflags="-X github.com/root-gg/plik/server/common.buildInfoString=$(BUILD_INFO)"

GO_BUILD=go build $(BUILD_FLAG)
GO_TEST=GORACE="halt_on_error=1" go test $(BUILD_FLAG) -race -cover -p 1

ifdef ENABLE_RACE_DETECTOR
build = $(race_detector)
else
build = go build
GO_BUILD:=GORACE="halt_on_error=1" $(GO_BUILD) -race
endif
test: build = $(race_detector)

all: clean clean-frontend frontend clients server

Expand All @@ -34,15 +36,15 @@ frontend:
# Build plik server for the current architecture
###
server:
@server/gen_build_info.sh $(RELEASE_VERSION)
@server/gen_build_info.sh $(RELEASE_VERSION) info
@echo "Building Plik server"
@cd server && $(build) -o plikd ./
@cd server && $(GO_BUILD) -o plikd

###
# Build plik server for all architectures
###
servers: frontend
@server/gen_build_info.sh $(RELEASE_VERSION)
@server/gen_build_info.sh $(RELEASE_VERSION) info
@cd server && for target in $(RELEASE_TARGETS) ; do \
SERVER_DIR=../servers/$$target; \
SERVER_PATH=$$SERVER_DIR/plikd; \
Expand All @@ -52,23 +54,23 @@ servers: frontend
if [ $$GOOS = "windows" ] ; then SERVER_PATH=$$SERVER_DIR/plikd.exe ; fi ; \
if [ -e $$SERVER_PATH ] ; then continue ; fi ; \
echo "Building Plik server for $$target to $$SERVER_PATH"; \
$(build) -o $$SERVER_PATH ; \
$(GO_BUILD) -o $$SERVER_PATH ; \
done

###
# Build plik client for the current architecture
###
client:
@server/gen_build_info.sh $(RELEASE_VERSION)
@server/gen_build_info.sh $(RELEASE_VERSION) info
@echo "Building Plik client"
@cd client && $(build) -o plik ./
@cd client && $(GO_BUILD) -o plik ./


###
# Build plik client for all architectures
###
clients:
@server/gen_build_info.sh $(RELEASE_VERSION)
@server/gen_build_info.sh $(RELEASE_VERSION) info
@cd client && for target in $(RELEASE_TARGETS) ; do \
CLIENT_DIR=../clients/$$target; \
CLIENT_PATH=$$CLIENT_DIR/plik; \
Expand All @@ -79,7 +81,7 @@ clients:
if [ $$GOOS = "windows" ] ; then CLIENT_PATH=$$CLIENT_DIR/plik.exe ; fi ; \
if [ -e $$CLIENT_PATH ] ; then continue ; fi ; \
echo "Building Plik client for $$target to $$CLIENT_PATH"; \
$(build) -o $$CLIENT_PATH ; \
$(GO_BUILD) -o $$CLIENT_PATH ; \
md5sum $$CLIENT_PATH | awk '{print $$1}' > $$CLIENT_MD5; \
done
@mkdir -p clients/bash && cp client/plik.sh clients/bash
Expand Down Expand Up @@ -185,7 +187,7 @@ releases: release-template servers
# Generate build info
###
build-info:
@server/gen_build_info.sh $(RELEASE_VERSION)
@server/gen_build_info.sh $(RELEASE_VERSION) info

###
# Run linters
Expand All @@ -210,8 +212,7 @@ fmt:
###
test:
@if curl -s 127.0.0.1:8080 > /dev/null ; then echo "Plik server probably already running" && exit 1 ; fi
@server/gen_build_info.sh $(RELEASE_VERSION)
@GORACE="halt_on_error=1" go test -race -cover -p 1 ./... 2>&1 | grep -v "no test files"; test $${PIPESTATUS[0]} -eq 0
@$(GO_TEST) ./... 2>&1 | grep -v "no test files"; test $${PIPESTATUS[0]} -eq 0
@echo "cli client integration tests :" && cd client && ./test.sh

###
Expand All @@ -230,7 +231,6 @@ docker:
# Remove server build files
###
clean:
@rm -rf server/common/version.go
@rm -rf server/plikd
@rm -rf client/plik
@rm -rf clients
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ To compile plik from sources, you'll need golang and npm installed on your syste
First, get the project and libs via go get :
```sh
$ go get github.com/root-gg/plik/server
go/src/github.com/root-gg/plik/server/handlers/misc.go:51: undefined: common.GetBuildInfo <== ignore this warning
$ cd $GOPATH/src/github.com/root-gg/plik/
```

Expand Down
71 changes: 35 additions & 36 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/BurntSushi/toml"
"github.com/docopt/docopt-go"
"github.com/mitchellh/go-homedir"

"github.com/root-gg/plik/server/common"
Expand Down Expand Up @@ -73,7 +74,7 @@ func LoadConfigFromFile(path string) (*CliConfig, error) {

// LoadConfig creates a new default configuration and override it with .plikrc fike.
// If .plikrc does not exist, ask domain, and create a new one in user HOMEDIR
func LoadConfig() (config *CliConfig, err error) {
func LoadConfig(opts docopt.Opts) (config *CliConfig, err error) {
// Load config file from environment variable
path := os.Getenv("PLIKRC")
if path != "" {
Expand Down Expand Up @@ -115,11 +116,9 @@ func LoadConfig() (config *CliConfig, err error) {

config = NewUploadConfig()

// Check if quiet mode ( you'll have to pass --server flag )
for _, arg := range os.Args[1:] {
if arg == "-q" || arg == "--quiet" {
return config, nil
}
// Bypass ~/.plikrc file creation if quiet mode and/or --server flag
if opts["--quiet"].(bool) || opts["--server"].(string) != "" {
return config, nil
}

// Config file not found. Create one.
Expand Down Expand Up @@ -192,22 +191,22 @@ func LoadConfig() (config *CliConfig, err error) {

// UnmarshalArgs turns command line arguments into upload settings
// Command line arguments override config file settings
func (config *CliConfig) UnmarshalArgs(arguments map[string]interface{}) (err error) {
if arguments["--debug"].(bool) {
func (config *CliConfig) UnmarshalArgs(opts docopt.Opts) (err error) {
if opts["--debug"].(bool) {
config.Debug = true
}
if arguments["--quiet"].(bool) {
if opts["--quiet"].(bool) {
config.Quiet = true
}

// Plik server url
if arguments["--server"] != nil && arguments["--server"].(string) != "" {
config.URL = arguments["--server"].(string)
if opts["--server"] != nil && opts["--server"].(string) != "" {
config.URL = opts["--server"].(string)
}

// Paths
if _, ok := arguments["FILE"].([]string); ok {
config.filePaths = arguments["FILE"].([]string)
if _, ok := opts["FILE"].([]string); ok {
config.filePaths = opts["FILE"].([]string)
} else {
return fmt.Errorf("No files specified")
}
Expand All @@ -226,29 +225,29 @@ func (config *CliConfig) UnmarshalArgs(arguments map[string]interface{}) (err er
}

// Override file name if specified
if arguments["--name"] != nil && arguments["--name"].(string) != "" {
config.filenameOverride = arguments["--name"].(string)
if opts["--name"] != nil && opts["--name"].(string) != "" {
config.filenameOverride = opts["--name"].(string)
}

// Upload options
if arguments["--oneshot"].(bool) {
if opts["--oneshot"].(bool) {
config.OneShot = true
}
if arguments["--removable"].(bool) {
if opts["--removable"].(bool) {
config.Removable = true
}

if arguments["--stream"].(bool) {
if opts["--stream"].(bool) {
config.Stream = true
}

if arguments["--comments"] != nil && arguments["--comments"].(string) != "" {
config.Comments = arguments["--comments"].(string)
if opts["--comments"] != nil && opts["--comments"].(string) != "" {
config.Comments = opts["--comments"].(string)
}

// Configure upload expire date
if arguments["--ttl"] != nil && arguments["--ttl"].(string) != "" {
ttlStr := arguments["--ttl"].(string)
if opts["--ttl"] != nil && opts["--ttl"].(string) != "" {
ttlStr := opts["--ttl"].(string)
mul := 1
if string(ttlStr[len(ttlStr)-1]) == "m" {
mul = 60
Expand All @@ -262,32 +261,32 @@ func (config *CliConfig) UnmarshalArgs(arguments map[string]interface{}) (err er
}
ttl, err := strconv.Atoi(ttlStr)
if err != nil {
return fmt.Errorf("Invalid TTL %s", arguments["--ttl"].(string))
return fmt.Errorf("Invalid TTL %s", opts["--ttl"].(string))
}
config.TTL = ttl * mul
}

// Enable archive mode ?
if arguments["-a"].(bool) || arguments["--archive"] != nil || config.Archive {
if opts["-a"].(bool) || opts["--archive"] != nil || config.Archive {
config.Archive = true

if arguments["--archive"] != nil && arguments["--archive"] != "" {
config.ArchiveMethod = arguments["--archive"].(string)
if opts["--archive"] != nil && opts["--archive"] != "" {
config.ArchiveMethod = opts["--archive"].(string)
}
}

// Enable secure mode ?
if arguments["--not-secure"].(bool) {
if opts["--not-secure"].(bool) {
config.Secure = false
} else if arguments["-s"].(bool) || arguments["--secure"] != nil || config.Secure {
} else if opts["-s"].(bool) || opts["--secure"] != nil || config.Secure {
config.Secure = true
if arguments["--secure"] != nil && arguments["--secure"].(string) != "" {
config.SecureMethod = arguments["--secure"].(string)
if opts["--secure"] != nil && opts["--secure"].(string) != "" {
config.SecureMethod = opts["--secure"].(string)
}
}

// Enable password protection ?
if arguments["-p"].(bool) {
if opts["-p"].(bool) {
fmt.Printf("Login [plik]: ")
var err error
_, err = fmt.Scanln(&config.Login)
Expand All @@ -302,8 +301,8 @@ func (config *CliConfig) UnmarshalArgs(arguments map[string]interface{}) (err er
if err != nil {
return fmt.Errorf("Unable to get password : %s", err)
}
} else if arguments["--password"] != nil && arguments["--password"].(string) != "" {
credentials := arguments["--password"].(string)
} else if opts["--password"] != nil && opts["--password"].(string) != "" {
credentials := opts["--password"].(string)
sepIndex := strings.Index(credentials, ":")
var login, password string
if sepIndex > 0 {
Expand All @@ -318,8 +317,8 @@ func (config *CliConfig) UnmarshalArgs(arguments map[string]interface{}) (err er
}

// Override upload token ?
if arguments["--token"] != nil && arguments["--token"].(string) != "" {
config.Token = arguments["--token"].(string)
if opts["--token"] != nil && opts["--token"].(string) != "" {
config.Token = opts["--token"].(string)
}

// Ask for token
Expand All @@ -332,7 +331,7 @@ func (config *CliConfig) UnmarshalArgs(arguments map[string]interface{}) (err er
}
}

if arguments["--stdin"].(bool) {
if opts["--stdin"].(bool) {
config.DisableStdin = false
}

Expand Down
19 changes: 11 additions & 8 deletions client/plik.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Options:
}

// Load config
config, err = LoadConfig()
config, err = LoadConfig(arguments)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to load configuration : %s\n", err)
os.Exit(1)
Expand All @@ -105,7 +105,12 @@ Options:

// Display info
if arguments["--info"].(bool) {
info(client)
err = info(client)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
os.Exit(0)
}

// Update
Expand Down Expand Up @@ -307,29 +312,27 @@ Options:
}
}

func info(client *plik.Client) {
func info(client *plik.Client) (err error) {
fmt.Printf("Plik client version : %s\n\n", common.GetBuildInfo())

fmt.Printf("Plik server url : %s\n", config.URL)

serverBuildInfo, err := client.GetServerVersion()
if err != nil {
fmt.Printf("Plik server unreachable %s\n", err)
return
return fmt.Errorf("Plik server unreachable : %s", err)
}

fmt.Printf("Plik server version : %s\n", serverBuildInfo)

serverConfig, err := client.GetServerConfig()
if err != nil {
fmt.Printf("Plik server unreachable %s\n", err)
return
return fmt.Errorf("Plik server unreachable : %s", err)
}

fmt.Printf("\nPlik server configuration :\n")
fmt.Printf(serverConfig.String())

os.Exit(0)
return nil
}

func getFileCommand(file *plik.File) (command string, err error) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/gorilla/mux v1.7.1
github.com/hashicorp/golang-lru v0.5.1 // indirect
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334 // indirect
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a
github.com/jinzhu/gorm v1.9.13-0.20200126152832-7180bd0f27d1
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
github.com/lib/pq v1.3.1-0.20200116171513-9eb3fc897d6f // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334 h1:VHgatEHNcBFE
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a h1:zPPuIq2jAWWPTrGt70eK/BSch+gFAGrNzecsoENgu2o=
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a/go.mod h1:yL958EeXv8Ylng6IfnvG4oflryUi3vgA3xPs9hmII1s=
github.com/jinzhu/gorm v1.9.2/go.mod h1:Vla75njaFJ8clLU1W44h34PjIkijhjHIYnZxMqCdxqo=
github.com/jinzhu/gorm v1.9.11/go.mod h1:bu/pK8szGZ2puuErfU0RwyeNdsf3e6nCX/noXaVxkfw=
github.com/jinzhu/gorm v1.9.13-0.20200126152832-7180bd0f27d1 h1:3FjnhIYXVZpGs9h4eA1mSe65h0ocfwoaO8ehiqW0sf8=
Expand Down
2 changes: 1 addition & 1 deletion server/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewConfiguration() (config *Configuration) {

config.ListenAddress = "0.0.0.0"
config.ListenPort = 8080
config.EnhancedWebSecurity = true
config.EnhancedWebSecurity = false

config.MaxFileSize = 10000000000 // 10GB
config.MaxFilePerUpload = 1000
Expand Down
Loading

0 comments on commit 156baf9

Please sign in to comment.