From 78c0c4d0b89814480f7e16a91aff0728096ad8ed Mon Sep 17 00:00:00 2001 From: Shingo Kawamura Date: Sat, 29 Jul 2017 13:46:25 +0900 Subject: [PATCH] Move main.go to cmd/scv/scv.go --- .gitignore | 3 +-- scv.sample.json => .scv.sample.json | 0 Makefile | 17 ++++++++++------- README.md | 4 ++-- api/api.go => api.go | 12 +++++++----- cmd/scv/scv.go | 18 ++++++++++++++++++ conf/conf.go => config.go | 2 +- conf/conf_test.go => config_test.go | 8 ++++---- logger/logger.go => logger.go | 2 +- main.go | 19 ------------------- cmd/cmd.go => option.go | 21 +++++++++------------ version => shellscripts/version.sh | 0 zip => shellscripts/zip.sh | 5 +++-- 13 files changed, 56 insertions(+), 55 deletions(-) rename scv.sample.json => .scv.sample.json (100%) rename api/api.go => api.go (75%) create mode 100644 cmd/scv/scv.go rename conf/conf.go => config.go (98%) rename conf/conf_test.go => config_test.go (94%) rename logger/logger.go => logger.go (98%) delete mode 100644 main.go rename cmd/cmd.go => option.go (77%) rename version => shellscripts/version.sh (100%) rename zip => shellscripts/zip.sh (88%) diff --git a/.gitignore b/.gitignore index a717e17..112091d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ -scv -archives/ +tmp/ ### https://raw.github.com/github/gitignore/c0c1a480a906df0e023f3250cf2ad82f1612be67/Go.gitignore diff --git a/scv.sample.json b/.scv.sample.json similarity index 100% rename from scv.sample.json rename to .scv.sample.json diff --git a/Makefile b/Makefile index 894f971..2f9087d 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,19 @@ -.PHONY: all zip build clean test +.PHONY: all zip build clean test tmp all: build zip: build - @./zip + @./shellscripts/zip.sh -build: - @go build -ldflags "-X github.com/blp1526/scv/cmd.version="$(shell ./version) +build: tmp + @mkdir -p tmp + @go build -o tmp/scv -ldflags "-X github.com/blp1526/scv.version="$(shell ./shellscripts/version.sh) cmd/scv/scv.go clean: - @go clean - @rm -rf archives + @rm -rf tmp test: - @go test -v ./... + @go test -v + +tmp: + @mkdir -p tmp diff --git a/README.md b/README.md index 963b5b6..326e556 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,8 @@ If you use this cli tool, you have to turn on a SAKURA Cloud server power. ## Usage -* Create a config file at `$HOME/scv.json`. -* Write a config file refering to [`scv.sample.json`](scv.sample.json). +* Create a config file at `$HOME/.scv.json`. +* Write a config file refering to [`.scv.sample.json`](.scv.sample.json). * This file's server name don't have to match the SAKURA cloud sever name. * Run below command. diff --git a/api/api.go b/api.go similarity index 75% rename from api/api.go rename to api.go index 07d85fc..10f9eb1 100644 --- a/api/api.go +++ b/api.go @@ -1,4 +1,4 @@ -package api +package scv import ( "encoding/json" @@ -7,11 +7,12 @@ import ( "time" ) -type Vnc struct { +type Api struct { ZoneName string ServerID string AccessToken string AccessTokenSecret string + Logger Logger } type Body struct { @@ -20,18 +21,19 @@ type Body struct { Port string `json:"Port"` } -func (vnc *Vnc) GetServerAddress() (serverAddress string, err error) { +func (api *Api) GetServerAddress() (serverAddress string, err error) { scheme := "https" host := "secure.sakura.ad.jp" - path := "/cloud/zone/" + vnc.ZoneName + "/api/cloud/1.1/server/" + vnc.ServerID + "/vnc/proxy" + path := "/cloud/zone/" + api.ZoneName + "/api/cloud/1.1/server/" + api.ServerID + "/vnc/proxy" url := scheme + "://" + host + path + api.Logger.Debug("URL: " + url) req, err := http.NewRequest("GET", url, nil) if err != nil { return serverAddress, err } - req.SetBasicAuth(vnc.AccessToken, vnc.AccessTokenSecret) + req.SetBasicAuth(api.AccessToken, api.AccessTokenSecret) client := &http.Client{Timeout: 10 * time.Second} resp, err := client.Do(req) if err != nil { diff --git a/cmd/scv/scv.go b/cmd/scv/scv.go new file mode 100644 index 0000000..b3d6e4e --- /dev/null +++ b/cmd/scv/scv.go @@ -0,0 +1,18 @@ +package main + +import ( + "os" + + "github.com/blp1526/scv" +) + +func main() { + logger := &scv.Logger{} + msg, err := scv.Run() + if err != nil { + logger.Fatal(err) + os.Exit(1) + } else { + logger.Info(msg) + } +} diff --git a/conf/conf.go b/config.go similarity index 98% rename from conf/conf.go rename to config.go index e880794..fea856f 100644 --- a/conf/conf.go +++ b/config.go @@ -1,4 +1,4 @@ -package conf +package scv import ( "encoding/json" diff --git a/conf/conf_test.go b/config_test.go similarity index 94% rename from conf/conf_test.go rename to config_test.go index 1258ca3..53d9b2e 100644 --- a/conf/conf_test.go +++ b/config_test.go @@ -1,4 +1,4 @@ -package conf +package scv import ( "path/filepath" @@ -6,8 +6,8 @@ import ( ) func TestLoadFile(t *testing.T) { - expectedPath, _ := filepath.Abs("../scv.sample.json") - unexpectedPath, _ := filepath.Abs("../scv.not.sample.json") + expectedPath, _ := filepath.Abs(".scv.sample.json") + unexpectedPath, _ := filepath.Abs(".scv.not.sample.json") tests := []struct { dir string want Config @@ -110,7 +110,7 @@ func TestGetServerID(t *testing.T) { }, } - absPath, _ := filepath.Abs("../scv.sample.json") + absPath, _ := filepath.Abs(".scv.sample.json") config := Config{} _ = config.LoadFile(absPath) diff --git a/logger/logger.go b/logger.go similarity index 98% rename from logger/logger.go rename to logger.go index e20783a..8731f45 100644 --- a/logger/logger.go +++ b/logger.go @@ -1,4 +1,4 @@ -package logger +package scv import "fmt" diff --git a/main.go b/main.go deleted file mode 100644 index 9df191c..0000000 --- a/main.go +++ /dev/null @@ -1,19 +0,0 @@ -package main - -import ( - "os" - - "github.com/blp1526/scv/cmd" - "github.com/blp1526/scv/logger" -) - -func main() { - l := &logger.Logger{} - msg, err := cmd.Run() - if err != nil { - l.Fatal(err) - os.Exit(1) - } else { - l.Info(msg) - } -} diff --git a/cmd/cmd.go b/option.go similarity index 77% rename from cmd/cmd.go rename to option.go index c5b692a..3e99fec 100644 --- a/cmd/cmd.go +++ b/option.go @@ -1,14 +1,10 @@ -package cmd +package scv import ( "flag" "fmt" "os/user" "path/filepath" - - "github.com/blp1526/scv/api" - "github.com/blp1526/scv/conf" - "github.com/blp1526/scv/logger" ) var version string @@ -26,9 +22,9 @@ func Run() (result string, err error) { if optVersion { return fmt.Sprintf("scv version %s", version), err } - l := &logger.Logger{} + logger := &Logger{} if optVerbose { - l.Verbose = true + logger.Verbose = true } argsSize := len(flag.Args()) @@ -41,9 +37,9 @@ func Run() (result string, err error) { serverName := flag.Args()[1] current, _ := user.Current() - dir := filepath.Join(current.HomeDir, "scv.json") + dir := filepath.Join(current.HomeDir, ".scv.json") - config := conf.Config{} + config := Config{} err = config.LoadFile(dir) if err != nil { return result, err @@ -53,15 +49,16 @@ func Run() (result string, err error) { if err != nil { return result, err } - l.Debug("ServerID is " + serverID) + logger.Debug("ServerID: " + serverID) - vnc := api.Vnc{ + api := Api{ ZoneName: zoneName, ServerID: serverID, AccessToken: config.AccessToken, AccessTokenSecret: config.AccessTokenSecret, + Logger: *logger, } - result, err = vnc.GetServerAddress() + result, err = api.GetServerAddress() if err != nil { return result, err } diff --git a/version b/shellscripts/version.sh similarity index 100% rename from version rename to shellscripts/version.sh diff --git a/zip b/shellscripts/zip.sh similarity index 88% rename from zip rename to shellscripts/zip.sh index 2c66779..8a85fd8 100755 --- a/zip +++ b/shellscripts/zip.sh @@ -18,8 +18,9 @@ else exit 1 fi -rm -rf "archives" -path="archives/scv_${GOOS}_${GOARCH}" +cd "tmp" +rm -rf "scv_*" +path="scv_${GOOS}_${GOARCH}" mkdir -p ${path} mv scv ${path} zip -r "${path}.zip" ${path} > /dev/null