Skip to content

Commit 56da680

Browse files
committed
add support to read the docker host from config file
1 parent 49c0d97 commit 56da680

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,13 @@ The stack name can be ommited, in that case the current directory name will be u
2828
* `--with-registry-auth, -a` Send registry authentication details to Swarm agents.
2929
* `--prune, -p` Prune services that are no longer referenced.
3030
* `--host, -H` Daemon socket(s) to connect to.
31+
32+
## Config file
33+
34+
A file named `.docker-deploy.yml` can be placed in the current directory or any of the parent directories.
35+
Currently, only the Docker host can be specified.
36+
37+
Example:
38+
```yaml
39+
host: ssh://user@example.org:port
40+
```

main.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ import (
1919
"gopkg.in/yaml.v2"
2020
)
2121

22+
type appConfig struct {
23+
Host string `yaml:"host"`
24+
}
25+
2226
type configSettings struct {
2327
Name string `yaml:"name"`
2428
File string `yaml:"file"`
@@ -132,6 +136,41 @@ func loadEnvFromConfigFile(filename string, stdin io.Reader) ([]string, error) {
132136
return environmentFromYaml(yamlFile)
133137
}
134138

139+
func loadAppConfig(filename string) (*appConfig, error) {
140+
cfg := &appConfig{}
141+
targetPath, err := os.Getwd()
142+
if err != nil {
143+
return nil, err
144+
}
145+
146+
rootDir := filepath.Join(filepath.VolumeName(targetPath), "/")
147+
148+
for {
149+
configPath := filepath.Join(targetPath, filename)
150+
data, err := ioutil.ReadFile(configPath)
151+
if err != nil {
152+
if os.IsNotExist(err) {
153+
if targetPath == rootDir {
154+
return cfg, nil
155+
}
156+
targetPath = filepath.Dir(targetPath)
157+
continue
158+
}
159+
break
160+
}
161+
162+
log.Printf("Reading config file: %s", configPath)
163+
err = yaml.Unmarshal(data, cfg)
164+
if err != nil {
165+
break
166+
}
167+
168+
return cfg, nil
169+
}
170+
171+
return nil, err
172+
}
173+
135174
var auth = flag.BoolP("with-registry-auth", "a", false, "Send registry authentication details to Swarm agents")
136175
var prune = flag.BoolP("prune", "p", false, "Prune services that are no longer referenced")
137176
var host = flag.StringP("host", "H", "", "Daemon socket(s) to connect to")
@@ -148,6 +187,11 @@ func main() {
148187
log.Fatal(err)
149188
}
150189

190+
cfg, err := loadAppConfig(".docker-deploy.yml")
191+
if err != nil {
192+
log.Fatal(err)
193+
}
194+
151195
var buf bytes.Buffer
152196
tee := io.TeeReader(os.Stdin, &buf)
153197
env, err := loadEnvFromConfigFiles(*composeFiles, tee)
@@ -162,6 +206,8 @@ func main() {
162206

163207
if *host != "" {
164208
args = append([]string{"--host", *host}, args...)
209+
} else if cfg.Host != "" {
210+
args = append([]string{"--host", cfg.Host}, args...)
165211
}
166212

167213
if *auth {

0 commit comments

Comments
 (0)