Skip to content
This repository has been archived by the owner on Dec 4, 2020. It is now read-only.

Commit

Permalink
CHORE: Start correcting code problems (#39)
Browse files Browse the repository at this point in the history
* Use go channels to hopefully not print before server dies

This PR does the following:
- Setup and block on the done channel
- Print message after spinning out the go server (the server dies if any exception happens), so we should be able to rely on seeing this message now
- Block for the connectws() consumers

Signed-off-by: Brandon Schneider <[email protected]>

* Also log to file when we listen

Signed-off-by: Brandon Schneider <[email protected]>

* We should also log to file any errors and fatally exit

Signed-off-by: Brandon Schneider <[email protected]>

* CHORE: Start correcting code problems

This also institutes a lint before we build. Along with typos.

Signed-off-by: Brandon Schneider <[email protected]>

* Get these deps when we make and split out dev/release building

Signed-off-by: Brandon Schneider <[email protected]>

* Let’s add install as well

Signed-off-by: Brandon Schneider <[email protected]>

* Export PATH to include GOPATH for our installed deps

Signed-off-by: Brandon Schneider <[email protected]>

* Allow running things from GOPATH expanded out to use bin/ and
tossed into PATH at runtime

* Include full PATH

* Switch to Mage

This is easier to maintain.

Signed-off-by: Brandon Schneider <[email protected]>

* Typo and fmt

Signed-off-by: Brandon Schneider <[email protected]>

* Add helper Makefile to run go run bootstrap.go targets for common
use cases, for us old-school folks who don't use mage

* Format

Signed-off-by: Brandon Schneider <[email protected]>
  • Loading branch information
Brandon Schneider authored May 13, 2019
1 parent 9a75da8 commit 9ef174d
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 52 deletions.
32 changes: 11 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
BINARY_NAME=sysup

# We will add test later
all: format build
all: build

build:
$(GOBUILD) -o $(BINARY_NAME) -v
format:
$(GOCMD) fmt ./...
test:
$(GOTEST) -v ./...
clean:
$(GOCLEAN)
rm -f $(BINARY_NAME)
run:
$(GOBUILD) -o $(BINARY_NAME) -v ./...
./$(BINARY_NAME)
go run bootstrap.go build

dev:
go run bootstrap.go dev

lint:
go run bootstrap.go lint

install:
go run bootstrap.go install
12 changes: 12 additions & 0 deletions bootstrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//+build bootstrap

package main

import (
"github.com/magefile/mage/mage"
"os"
)

func main() {
os.Exit(mage.Main())
}
12 changes: 6 additions & 6 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ func printtrains(trains []defines.TrainDef, deftrain string) {
log.Println(
"------------------------------------------------------------------",
)
for i, _ := range trains {
for i := range trains {
log.Printf("%s\t\t\t%s", trains[i].Name, trains[i].Description)
if trains[i].Deprecated {
log.Printf(" [Deprecated]")
}
for j, _ := range trains[i].Tags {
for j := range trains[i].Tags {
log.Printf(" [%s]", trains[i].Tags[j])
}
log.Printf("\n")
Expand Down Expand Up @@ -251,7 +251,7 @@ func printupdatedetails(details defines.UpdateInfo) {

log.Println("The following packages will be updated:")
log.Println("----------------------------------------------------")
for i, _ := range details.Up {
for i := range details.Up {
log.Println(
" " + details.Up[i].Name + " " + details.Up[i].OldVersion +
" -> " + details.Up[i].NewVersion,
Expand All @@ -261,21 +261,21 @@ func printupdatedetails(details defines.UpdateInfo) {
log.Println()
log.Println("The following packages will be installed:")
log.Println("----------------------------------------------------")
for i, _ := range details.New {
for i := range details.New {
log.Println(" " + details.New[i].Name + " " + details.New[i].Version)
}

log.Println()
log.Println("The following packages will be reinstalled:")
log.Println("----------------------------------------------------")
for i, _ := range details.Ri {
for i := range details.Ri {
log.Println(" " + details.Ri[i].Name + " " + details.Ri[i].Reason)
}

log.Println()
log.Println("The following packages will be removed:")
log.Println("----------------------------------------------------")
for i, _ := range details.Del {
for i := range details.Del {
log.Println(" " + details.Del[i].Name + " " + details.Del[i].Version)
}
}
Expand Down
2 changes: 1 addition & 1 deletion defines/defines.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func init() {
&ChangeTrainFlag,
"change-train",
"",
"Change to the specifed new train",
"Change to the specified new train",
)
flag.BoolVar(
&FullUpdateFlag,
Expand Down
14 changes: 13 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,16 @@ module github.com/trueos/sysup

go 1.12

require github.com/gorilla/websocket v1.4.0
require (
github.com/client9/misspell v0.3.4 // indirect
github.com/fzipp/gocyclo v0.0.0-20150627053110-6acd4345c835 // indirect
github.com/gordonklaus/ineffassign v0.0.0-20180909121442-1003c8bd00dc // indirect
github.com/gorilla/websocket v1.4.0
github.com/magefile/mage v1.8.0
golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284 // indirect
golang.org/x/lint v0.0.0-20190409202823-959b441ac422 // indirect
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c // indirect
golang.org/x/sys v0.0.0-20190509141414-a5b02f93d862 // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/tools v0.0.0-20190509153222-73554e0f7805 // indirect
)
25 changes: 25 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/fzipp/gocyclo v0.0.0-20150627053110-6acd4345c835 h1:roDmqJ4Qes7hrDOsWsMCce0vQHz3xiMPjJ9m4c2eeNs=
github.com/fzipp/gocyclo v0.0.0-20150627053110-6acd4345c835/go.mod h1:BjL/N0+C+j9uNX+1xcNuM9vdSIcXCZrQZUYbXOFbgN8=
github.com/gordonklaus/ineffassign v0.0.0-20180909121442-1003c8bd00dc h1:cJlkeAx1QYgO5N80aF5xRGstVsRQwgLR7uA2FnP1ZjY=
github.com/gordonklaus/ineffassign v0.0.0-20180909121442-1003c8bd00dc/go.mod h1:cuNKsD1zp2v6XfE/orVX2QE1LC+i254ceGcVeDT3pTU=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/magefile/mage v1.8.0 h1:mzL+xIopvPURVBwHG9A50JcjBO+xV3b5iZ7khFRI+5E=
github.com/magefile/mage v1.8.0/go.mod h1:IUDi13rsHje59lecXokTfGX0QIzO45uVPlXnJYsXepA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20190409202823-959b441ac422 h1:QzoH/1pFpZguR8NrRHLcO6jKqfv2zpuSqZLgdm7ZmjI=
golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190509141414-a5b02f93d862/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190509153222-73554e0f7805 h1:1ufBXAsTpUhSmmPXEEs5PrGQSfnBhsjAd2SmVhp9xrY=
golang.org/x/tools v0.0.0-20190509153222-73554e0f7805/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
85 changes: 85 additions & 0 deletions magefile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//+build mage

package main

import (
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"log"
)

// TODO: Use Test() later

// Uses go fmt to format all our modules
func Format() {
log.Println("Running format")

sh.RunV("go", "fmt", "./...")
}

// Installs our formatting deps
func InstallDeps() {
log.Println("Installing deps")
sh.Run(
"go", "get", "github.com/fzipp/gocyclo",
"github.com/gordonklaus/ineffassign", "github.com/client9/misspell",
"golang.org/x/lint/golint",
)
}

// Runs the linting process
func Lint() {
log.Println("Running lint")
sh.RunV("go", "vet", "./...")
sh.RunV(
"gocyclo", "-over", "15", "pkg", "logger", "trains", "update",
"utils", "ws", "defines", "client",
)
sh.RunV("golint", "./...")
sh.RunV("ineffassign", "./")
sh.RunV("misspell", "-w", "./")
}

// Cleans sysups binary up
func Clean() {
log.Println("Running clean")
sh.RunV("go", "clean")
sh.RunV("rm", "-f", "./sysup")
}

// Builds sysup and run it from this directory
func Run() {
log.Println("Running run")
mg.Deps(Build)
sh.Run("./sysup")
}

// Installs sysup to GOPATH
func Install() {
log.Println("Running install")
sh.RunV("go", "install")
}

// Run our tests (nonfunctional)
func Test() {
log.Println("Running test")
sh.RunV("go", "test", "-v", "./...")
}

// Builds sysup and install it
func All() {
log.Println("Making all")
mg.Deps(Build, Install)
}

// Builds sysup
func Build() {
log.Println("Making build")
sh.RunV("go", "build", "-o", "sysup", "-v")
}

// Standard development build process (format, install deps, lint, build)
func Dev() {
log.Println("Making dev")
mg.Deps(Format, InstallDeps, Lint, Build)
}
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func startws(done chan bool) {
log.SetFlags(0)
http.HandleFunc("/ws", readws)

if err := http.ListenAndServe(defines.WebsocketAddr, nil) ; err != nil {
logger.LogToFile("ERROR: " + err.Error())
log.Fatal(err)
if err := http.ListenAndServe(defines.WebsocketAddr, nil); err != nil {
logger.LogToFile("ERROR: " + err.Error())
log.Fatal(err)
}

done <- true
Expand Down
8 changes: 4 additions & 4 deletions pkg/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func GetRemoteOsVer() (string, error) {
return string(strarray[1]), nil
}
}
return "", fmt.Errorf("Failed to get FreeBSD_version", allText)
return "", fmt.Errorf("Failed to get FreeBSD_version %s", allText)
}

func mountofflineupdate() {
Expand Down Expand Up @@ -286,7 +286,7 @@ func UpdateDryRun(sendupdate bool) (*defines.UpdateInfo, bool, error) {
allText = append(allText, buff.Text()+"\n")
}
//log.Println(allText)
// Pkg returns 0 on sucess and 1 on updates needed
// Pkg returns 0 on success and 1 on updates needed
//if err := cmd.Wait(); err != nil {
// log.Fatal(err)
//}
Expand Down Expand Up @@ -404,7 +404,7 @@ func ParseUpdateData(uptext []string) *defines.UpdateInfo {
details.KernelPkg = kernel
details.KernelUp = false
log.Println("Kernel: " + kernel)
for i, _ := range details.Up {
for i := range details.Up {
if details.Up[i].Name == kernel {
// Set JSON details on the kernel update
details.KernelUp = true
Expand All @@ -415,7 +415,7 @@ func ParseUpdateData(uptext []string) *defines.UpdateInfo {
// Check if we have a SysUp package to update
details.SysUpPkg = ""
details.SysUp = false
for i, _ := range details.Up {
for i := range details.Up {
if details.Up[i].Name == "sysup" {
// Set JSON details on the sysup package
details.SysUp = true
Expand Down
4 changes: 2 additions & 2 deletions trains/trains.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func createnewpkgconf(train defines.TrainDef) {

// Write the new key file
var kdata []string
for i, _ := range train.PkgKey {
for i := range train.PkgKey {
kdata = append(kdata, train.PkgKey[i])
}
ioutil.WriteFile(
Expand Down Expand Up @@ -239,7 +239,7 @@ func DoSetTrain(message []byte) {

var foundt = -1
trains := trainlist.Trains
for i, _ := range trains {
for i := range trains {
if trains[i].Name == newtrain {
foundt = i
break
Expand Down
Loading

0 comments on commit 9ef174d

Please sign in to comment.