Skip to content

Commit

Permalink
Map for config.Remotes, add example inertia.toml (#282)
Browse files Browse the repository at this point in the history
* Prevent duplicate remotes from being added

* Use map for config instead of array

* Standardize TOML field naming

* Add example Inertia configuration

* Cleaner 'for' loop
  • Loading branch information
bobheadxi committed Jun 29, 2018
1 parent 69c0e0e commit b553ace
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 25 deletions.
34 changes: 19 additions & 15 deletions cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ var (

// Config represents the current projects configuration.
type Config struct {
Version string `toml:"version"`
Project string `toml:"project-name"`
BuildType string `toml:"build-type"`
BuildFilePath string `toml:"build-file-path"`
Remotes []*RemoteVPS `toml:"remote"`
Version string `toml:"version"`
Project string `toml:"project-name"`
BuildType string `toml:"build-type"`
BuildFilePath string `toml:"build-file-path"`

Remotes map[string]*RemoteVPS `toml:"remotes"`
}

// NewConfig sets up Inertia configuration with given properties
Expand All @@ -29,7 +30,7 @@ func NewConfig(version, project, buildType, buildFilePath string) *Config {
Version: version,
Project: project,
BuildType: buildType,
Remotes: make([]*RemoteVPS, 0),
Remotes: make(map[string]*RemoteVPS),
}
if buildFilePath != "" {
cfg.BuildFilePath = buildFilePath
Expand Down Expand Up @@ -89,18 +90,21 @@ func (config *Config) GetRemote(name string) (*RemoteVPS, bool) {
}

// AddRemote adds a remote to configuration
func (config *Config) AddRemote(remote *RemoteVPS) {
config.Remotes = append(config.Remotes, remote)
func (config *Config) AddRemote(remote *RemoteVPS) bool {
_, ok := config.Remotes[remote.Name]
if ok {
return false
}
config.Remotes[remote.Name] = remote
return true
}

// RemoveRemote removes remote with given name
func (config *Config) RemoveRemote(name string) bool {
for index, remote := range config.Remotes {
if remote.Name == name {
remote = nil
config.Remotes = append(config.Remotes[:index], config.Remotes[index+1:]...)
return true
}
_, ok := config.Remotes[name]
if !ok {
return false
}
return false
delete(config.Remotes, name)
return true
}
8 changes: 6 additions & 2 deletions cfg/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestWriteToWritersAndFile(t *testing.T) {
}

func TestConfigGetRemote(t *testing.T) {
config := &Config{Remotes: make([]*RemoteVPS, 0)}
config := &Config{Remotes: make(map[string]*RemoteVPS)}
testRemote := &RemoteVPS{
Name: "test",
IP: "12343",
Expand All @@ -85,7 +85,7 @@ func TestConfigGetRemote(t *testing.T) {
}

func TestConfigRemoveRemote(t *testing.T) {
config := &Config{Remotes: make([]*RemoteVPS, 0)}
config := &Config{Remotes: make(map[string]*RemoteVPS)}
testRemote := &RemoteVPS{
Name: "test",
IP: "12343",
Expand All @@ -97,6 +97,10 @@ func TestConfigRemoveRemote(t *testing.T) {
},
}
config.AddRemote(testRemote)

added := config.AddRemote(testRemote)
assert.False(t, added)

config.AddRemote(&RemoteVPS{
Name: "test2",
IP: "12343",
Expand Down
4 changes: 2 additions & 2 deletions cfg/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ type RemoteVPS struct {
User string `toml:"user"`
PEM string `toml:"pemfile"`
Branch string `toml:"branch"`
SSHPort string `toml:"ssh_port"`
SSHPort string `toml:"ssh-port"`
Daemon *DaemonConfig `toml:"daemon"`
}

// DaemonConfig contains parameters for the Daemon
type DaemonConfig struct {
Port string `toml:"port"`
Token string `toml:"token"`
WebHookSecret string `toml:"webhook_secret"`
WebHookSecret string `toml:"webhook-secret"`
}

// GetHost creates the user@IP string.
Expand Down
2 changes: 1 addition & 1 deletion client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestGetNewClient(t *testing.T) {
config := &cfg.Config{
Version: "test",
Project: "robert-writes-bad-code",
Remotes: make([]*cfg.RemoteVPS, 0),
Remotes: make(map[string]*cfg.RemoteVPS),
}
testRemote := &cfg.RemoteVPS{
Name: "test",
Expand Down
28 changes: 28 additions & 0 deletions example.inertia.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
version = "latest"
project-name = "inertia"
build-type = "dockerfile"
build-file-path = "Dockerfile"

[remote]
[remote.production]
name = "production"
IP = "1.2.3.4"
user = "root"
pemfile = "/Users/robertlin/.ssh/id_rsa"
branch = "master"
ssh_port = "22"
[remote.production.daemon]
port = "4303"
token = "asdfasdf"
webhook_secret = "9oC_5rfFcx2es4NLonMP2leOlJARkmu404EAXHXR4CI="
[remote.staging]
name = "staging"
IP = "4.3.2.1"
user = "bleh"
pemfile = "/Users/robertlin/.ssh/id_rsa"
branch = "dev"
ssh_port = "22"
[remote.staging.daemon]
port = "4303"
token = "asdfasdf"
webhook_secret = "7dtg2lmTHF5EDBUgIpdu0zuERqs9Emn8rd3NCh5EiMU="
5 changes: 4 additions & 1 deletion input.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func addRemoteWalkthrough(
fmt.Println("Run 'inertia remote add' with the -p flag to set a custom Daemon port")
fmt.Println("of the -ssh flag to set a custom SSH port.")

config.AddRemote(&cfg.RemoteVPS{
added := config.AddRemote(&cfg.RemoteVPS{
Name: name,
IP: address,
User: user,
Expand All @@ -86,6 +86,9 @@ func addRemoteWalkthrough(
WebHookSecret: secret,
},
})
if !added {
return errors.New("failed to add remote - you already have a remote with given name")
}
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions local/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func TestConfigCreateAndWriteAndRead(t *testing.T) {
// Test config read
readConfig, _, err := GetProjectConfigFromDisk("inertia.toml")
assert.Nil(t, err)
assert.Equal(t, config.Remotes[0], readConfig.Remotes[0])
assert.Equal(t, config.Remotes[1], readConfig.Remotes[1])
assert.Equal(t, config.Remotes["test"], readConfig.Remotes["test"])
assert.Equal(t, config.Remotes["test2"], readConfig.Remotes["test2"])

// Test client read
client, err := GetClient("test2", "inertia.toml")
Expand Down
4 changes: 2 additions & 2 deletions provision/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (p *EC2Provisioner) CreateInstance(opts EC2CreateInstanceOptions) (*cfg.Rem
println("Checking status of requested instance...")
attempts := 0
var instanceStatus *ec2.DescribeInstancesOutput
for true {
for {
attempts++
// Request instance status
result, err := p.client.DescribeInstances(&ec2.DescribeInstancesInput{
Expand Down Expand Up @@ -210,7 +210,7 @@ func (p *EC2Provisioner) CreateInstance(opts EC2CreateInstanceOptions) (*cfg.Rem

// Poll for SSH port to open
println("Waiting for port 22 to open...")
for true {
for {
time.Sleep(3 * time.Second)
println("Checking port...")
conn, err := net.Dial("tcp", *instanceStatus.Reservations[0].Instances[0].PublicDnsName+":22")
Expand Down

0 comments on commit b553ace

Please sign in to comment.