Skip to content

Commit

Permalink
refactor: improved init, restructure repo
Browse files Browse the repository at this point in the history
  • Loading branch information
tmountain committed Feb 3, 2021
1 parent f75b386 commit c637cfe
Show file tree
Hide file tree
Showing 22 changed files with 128 additions and 121 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ man: docs/uchess.txt
$(TXTMAN) -s1 -p -P uchess -t uchess docs/uchess.txt > docs/uchess.man

install: tidy
cd ./cmd/uchess && $(GO_BIN) install
cd cmd/uchess && $(GO_BIN) install
make tidy

tidy:
$(GO_BIN) mod tidy -v

build: tidy
pkger -o ./cmd/uchess
cd ./cmd/uchess && $(GO_BIN) build -v .
pkger -o cmd/uchess
cd cmd/uchess && $(GO_BIN) build -v .
make tidy

clean:
rm -f cmd/uchess/uchess
rm -rf dist

release:
rm -rf dist && goreleaser

Expand Down
2 changes: 1 addition & 1 deletion cmd/dbg/dbg.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"io/ioutil"

"github.com/tmountain/uchess"
uchess "github.com/tmountain/uchess/pkg"
)

func check(e error) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/uchess/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/gdamore/tcell/v2"
"github.com/notnil/chess"
"github.com/tmountain/uchess"
uchess "github.com/tmountain/uchess/pkg"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/uchess/pkged.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/tmountain/uchess
go 1.13

require (
github.com/artdarek/go-unzip v1.0.0
github.com/codeclysm/extract/v3 v3.0.2
github.com/dustin/go-humanize v1.0.0
github.com/freeeve/uci v1.0.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
github.com/ajstarks/svgo v0.0.0-20200320125537-f189e35d30ca h1:kWzLcty5V2rzOqJM7Tp/MfSX0RMSI1x4IOLApEefYxA=
github.com/ajstarks/svgo v0.0.0-20200320125537-f189e35d30ca/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/arduino/go-paths-helper v1.2.0 h1:qDW93PR5IZUN/jzO4rCtexiwF8P4OIcOmcSgAYLZfY4=
github.com/arduino/go-paths-helper v1.2.0/go.mod h1:HpxtKph+g238EJHq4geEPv9p+gl3v5YYu35Yb+w31Ck=
github.com/artdarek/go-unzip v1.0.0 h1:Ja9wfhiXyl67z5JT37rWjTSb62KXDP+9jHRkdSREUvg=
github.com/artdarek/go-unzip v1.0.0/go.mod h1:KhX4LV7e4UwWCTo7orBYnJ6LJ/dZTI6jXxUg69hO/C8=
github.com/codeclysm/extract v1.1.1 h1:rgayx3un6ZlMpyF6bTJI2uaaOg1rF9+8m3/T3OfE+70=
github.com/codeclysm/extract v2.2.0+incompatible h1:q3wyckoA30bhUSiwdQezMqVhwd8+WGE64/GL//LtUhI=
github.com/codeclysm/extract/v3 v3.0.2 h1:sB4LcE3Php7LkhZwN0n2p8GCwZe92PEQutdbGURf5xc=
github.com/codeclysm/extract/v3 v3.0.2/go.mod h1:NKsw+hqua9H+Rlwy/w/3Qgt9jDonYEgB6wJu+25eOKw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -26,8 +23,10 @@ github.com/juju/clock v0.0.0-20180524022203-d293bb356ca4/go.mod h1:nD0vlnrUjcjJh
github.com/juju/errors v0.0.0-20150916125642-1b5e39b83d18/go.mod h1:W54LbzXuIE0boCoNJfwqpmkKJ1O4TCTZMetAt6jGk7Q=
github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5 h1:rhqTjzJlm7EbkELJDKMTU7udov+Se0xZkWmugr6zGok=
github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5/go.mod h1:W54LbzXuIE0boCoNJfwqpmkKJ1O4TCTZMetAt6jGk7Q=
github.com/juju/loggo v0.0.0-20170605014607-8232ab8918d9 h1:Y+lzErDTURqeXqlqYi4YBYbDd7ycU74gW1ADt57/bgY=
github.com/juju/loggo v0.0.0-20170605014607-8232ab8918d9/go.mod h1:vgyd7OREkbtVEN/8IXZe5Ooef3LQePvuBm9UWj6ZL8U=
github.com/juju/retry v0.0.0-20160928201858-1998d01ba1c3/go.mod h1:OohPQGsr4pnxwD5YljhQ+TZnuVRYpa5irjugL1Yuif4=
github.com/juju/testing v0.0.0-20200510222523-6c8c298c77a0 h1:+WWUkhnTjV6RNOxkcwk79qrjeyHEHvBzlneueBsatX4=
github.com/juju/testing v0.0.0-20200510222523-6c8c298c77a0/go.mod h1:hpGvhGHPVbNBraRLZEhoQwFLMrjK8PSlO4D3nDjKYXo=
github.com/juju/utils v0.0.0-20180808125547-9dfc6dbfb02b/go.mod h1:6/KLg8Wz/y2KVGWEpkK9vMNGkOnu4k/cqs8Z1fKjTOk=
github.com/juju/version v0.0.0-20161031051906-1f41e27e54f2/go.mod h1:kE8gK5X0CImdr7qpSKl3xB2PmpySSmfj7zVbkZFs81U=
Expand Down Expand Up @@ -65,6 +64,7 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b h1:QRR6H1YWRnHb4Y/HeNFCTJLFVxaq6wH4YuVdsUOr75U=
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/mgo.v2 v2.0.0-20160818015218-f2b6f6c918c4 h1:hILp2hNrRnYjZpmIbx70psAHbBSEcQ1NIzDcUbJ1b6g=
gopkg.in/mgo.v2 v2.0.0-20160818015218-f2b6f6c918c4/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
gopkg.in/yaml.v2 v2.0.0-20170712054546-1be3d31502d6/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
File renamed without changes.
72 changes: 44 additions & 28 deletions config.go → pkg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"

"github.com/markbates/pkger"
)
Expand Down Expand Up @@ -40,19 +41,18 @@ var DefaultOptions = []Option{
{"skill level", "20"}, // stockfish skill level
}

var uciEngines = []UCIEngine{
{
"stockfish", // Name
FindOrFetchStockfish(), // Path
128, // Hash
false, // Ponder
false, // OwnBook
4, // MultiPV
0, // Depth
"", // SearchMoves
3000, // MoveTime
DefaultOptions,
},
// DefaultEngine is a placeholder for the default UCI engine
var defaultEngine = UCIEngine{
"stockfish", // Name
"", // Path to UCI (filled in dynamically)
128, // Hash
false, // Ponder
false, // OwnBook
4, // MultiPV
0, // Depth
"", // SearchMoves
3000, // MoveTime
DefaultOptions,
}

// defaultFEN is the default board position
Expand Down Expand Up @@ -82,27 +82,43 @@ func ReadThemes() []ThemeHex {
}

// DefaultConfig defines the default configuration
var DefaultConfig = Config{
"stockfish", // UCIWhite
"stockfish", // UCIBlack
"stockfish", // UCIHint
uciEngines, // UCIEngine
defaultFEN, // FEN
"basic", // ActiveTheme
[]ThemeHex{}, // Themes
"human", // WhitePiece
"cpu", // BlackPiece
"", // WhiteName
"", // BlackName
var defaultConfig = Config{
"stockfish", // UCIWhite
"stockfish", // UCIBlack
"stockfish", // UCIHint
[]UCIEngine{}, // UCIEngine
defaultFEN, // FEN
"basic", // ActiveTheme
[]ThemeHex{}, // Themes
"human", // WhitePiece
"cpu", // BlackPiece
"", // WhiteName
"", // BlackName
}

// MakeDefault creates the default config
func MakeDefault() Config {
config := defaultConfig
engine := defaultEngine
// Try to find stockfish in the path
stockfish := FindStockfish()

// If stockfish cannot be found, set the config to the AppDir
if stockfish == "" {
stockfish = path.Join(AppDir(), StockfishFilename())
}

engine.Path = stockfish
config.UCIEngines = []UCIEngine{engine}
return config
}

// ConfigJSON returns the JSON encoded representation of the config
func ConfigJSON() string {
config := DefaultConfig
func ConfigJSON(config Config) string {
// Just include the basic theme in the default config as a reference
config.Themes = []ThemeHex{ThemeBasic.Hex()}

c, err := json.MarshalIndent(&DefaultConfig, "", " ")
c, err := json.MarshalIndent(&config, "", " ")
if err != nil {
panic(err)
}
Expand Down
59 changes: 35 additions & 24 deletions download.go → pkg/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ func MatchStockfishWin(file string) bool {
return stockfishExe.MatchString(file)
}

// FindStockfish scans a directory and looks for a stockfish binary. Upon
// SearchDirForStockfish scans a directory and looks for a stockfish binary. Upon
// matching, a string with the binary name is returned. If no match is found,
// an empty string is returned
func FindStockfish(dir string) string {
func SearchDirForStockfish(dir string) string {
files, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Println(err.Error())
Expand Down Expand Up @@ -190,7 +190,8 @@ func findTmpStockfish(dlDir string) (string, error) {
return "", nil
}

func stockfishFilename() string {
// StockfishFilename returns a platform specific filename for the Stockfish binary
func StockfishFilename() string {
if runtime.GOOS == "windows" {
return "stockfish.exe"
}
Expand Down Expand Up @@ -237,7 +238,7 @@ func downloadStockfish() bool {

// Move and rename the extracted binary
if uchessDir := ensureUchessDir(); uchessDir != "" {
finalPath := path.Join(uchessDir, stockfishFilename())
finalPath := path.Join(uchessDir, StockfishFilename())
if err = os.Rename(sfPath, finalPath); err != nil {
fmt.Println(err.Error())
return false
Expand All @@ -253,40 +254,50 @@ func uchessDirName() string {
return ".uchess"
}

// AppDir returns the uchess directory location
func AppDir() string {
homeDir, err := homedir.Dir()

if err != nil {
fmt.Println(err.Error())
return ""
}
return path.Join(homeDir, uchessDirName())
}

func pathDelim() string {
if runtime.GOOS == "windows" {
return ";"
}
return ":"
}

// FindOrFetchStockfish attempts to load Stockfish via the path and attempts
// to download the binary for a limited number of platforms
func FindOrFetchStockfish() string {
// FindStockfish attempts to find Stockfish in the PATH and config dir
// A string with the path is returned when found, an empty string is returned otherwise
func FindStockfish() string {
pathDelim := pathDelim()
osPath := os.Getenv("PATH")
paths := strings.Split(osPath, pathDelim)
homeDir, err := homedir.Dir()
uchessDir := path.Join(homeDir, uchessDirName())

if err != nil {
fmt.Println(err.Error())
} else {
paths = append(paths, uchessDir)
appDir := AppDir()
if appDir != "" && FileExists(appDir) {
paths = append(paths, appDir)
}

foundStockfish := false
fmt.Println("Looking for stockfish...")
for _, p := range paths {
fmt.Printf("Check: %v\n", p)
searchResult := FindStockfish(p)
searchResult := SearchDirForStockfish(p)
if searchResult != "" {
fmt.Printf("Found: %v%v%v\n", p, string(os.PathSeparator), searchResult)
return path.Join(p, searchResult)
}
}
return ""
}

// FetchStockfish attempts to load Stockfish via the path and attempts
// to download the binary for a limited number of platforms
func FetchStockfish() string {
stockfish := FindStockfish()

if !foundStockfish {
if stockfish == "" {
fmt.Printf("\nStockfish could not be found in your path.\n")

if (runtime.GOOS == "windows" || runtime.GOOS == "linux") && runtime.GOARCH == "amd64" {
Expand All @@ -302,22 +313,22 @@ func FindOrFetchStockfish() string {
success := downloadStockfish()

if success {
fmt.Println("Install was successful. Restart uchess.")
fmt.Printf("Install was successful. Restart uchess.\n\n")
os.Exit(0)
} else {
fmt.Println("Install was unsuccessful. Please install Stockfish manually.")
fmt.Printf("Install was unsuccessful. Please install Stockfish manually.\n\n")
os.Exit(0)
}
} else {
fmt.Println("Please see the docs for manual Stockfish configuration.")
fmt.Printf("Please see the docs for manual Stockfish configuration.\n\n")
os.Exit(0)
}
} else {
fmt.Println("Automated installation is not supported on your platform.")
fmt.Println("Please install Stockfish using your package manager ")
fmt.Println("or download a binary from the official website and ")
fmt.Printf("make sure the executable is in your path.\n\n")
fmt.Println("https://stockfishchess.org/download/")
fmt.Printf("https://stockfishchess.org/download/\n\n")
os.Exit(0)
}
}
Expand Down
File renamed without changes.
12 changes: 10 additions & 2 deletions init.go → pkg/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func Init() Config {
flag.Parse()

if *tmpl {
fmt.Println(ConfigJSON())
defaultCfg := MakeDefault()
fmt.Println(ConfigJSON(defaultCfg))
os.Exit(0)
}

Expand All @@ -73,14 +74,21 @@ func Init() Config {

// Load config file if provided
// Override command line flags for black/white
// If UCI engine is specified in config, it is taken at face value
if *cfg != "" {
config = ReadConfig(*cfg)
*white = config.WhitePiece
*black = config.BlackPiece
} else {
// Zero configuration config (hopefully)
config = DefaultConfig
config = MakeDefault()
config.Themes = ReadThemes()
uciPath := config.UCIEngines[0].Path

// If the UCI engine cannot be found, prompt to install if applicable
if !IsFile(uciPath) {
FetchStockfish()
}
}

config.WhitePiece = *white
Expand Down
File renamed without changes.
Loading

0 comments on commit c637cfe

Please sign in to comment.