Skip to content

Commit

Permalink
Merge pull request #28 from blp1526/flat_dir
Browse files Browse the repository at this point in the history
Move main.go to cmd/scv/scv.go
  • Loading branch information
blp1526 authored Jul 29, 2017
2 parents e6d129e + 78c0c4d commit 9190b40
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 55 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
scv
archives/
tmp/

### https://raw.github.com/github/gitignore/c0c1a480a906df0e023f3250cf2ad82f1612be67/Go.gitignore

Expand Down
File renamed without changes.
17 changes: 10 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
12 changes: 7 additions & 5 deletions api/api.go → api.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package api
package scv

import (
"encoding/json"
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions cmd/scv/scv.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion conf/conf.go → config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package conf
package scv

import (
"encoding/json"
Expand Down
8 changes: 4 additions & 4 deletions conf/conf_test.go → config_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package conf
package scv

import (
"path/filepath"
"testing"
)

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
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion logger/logger.go → logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package logger
package scv

import "fmt"

Expand Down
19 changes: 0 additions & 19 deletions main.go

This file was deleted.

21 changes: 9 additions & 12 deletions cmd/cmd.go → option.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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())
Expand All @@ -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
Expand All @@ -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
}
Expand Down
File renamed without changes.
5 changes: 3 additions & 2 deletions zip → shellscripts/zip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9190b40

Please sign in to comment.