forked from tsuru/tsuru
/
command.go
67 lines (58 loc) · 1.71 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2013 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"github.com/pkg/errors"
"github.com/tsuru/config"
"github.com/tsuru/gnuflag"
"github.com/tsuru/tsuru/api"
"github.com/tsuru/tsuru/cmd"
)
type configFile struct {
value string
}
func (v *configFile) String() string {
return v.value
}
func (v *configFile) Set(value string) error {
v.value = value
configPath = value
return nil
}
type tsurudCommand struct {
cmd.Command
fs *gnuflag.FlagSet
file configFile
}
func (c *tsurudCommand) Flags() *gnuflag.FlagSet {
if c.fs == nil {
if f, ok := c.Command.(cmd.FlaggedCommand); ok {
c.fs = f.Flags()
} else {
c.fs = gnuflag.NewFlagSet("tsurud", gnuflag.ExitOnError)
}
c.fs.Var(&c.file, "config", "Path to configuration file (default to /etc/tsuru/tsuru.conf)")
c.fs.Var(&c.file, "c", "Path to configuration file (default to /etc/tsuru/tsuru.conf)")
}
return c.fs
}
func (c *tsurudCommand) Run(context *cmd.Context, client *cmd.Client) error {
fmt.Fprintf(context.Stderr, "Opening config file: %s\n", configPath)
err := config.ReadConfigFile(configPath)
if err != nil {
msg := `Could not open tsuru config file at %s (%s).
For an example, see: tsuru/etc/tsuru.conf
Note that you can specify a different config file with the --config option -- e.g.: --config=./etc/tsuru.conf
`
fmt.Fprintf(context.Stderr, msg, configPath, err)
return err
}
fmt.Fprintf(context.Stderr, "Done reading config file: %s\n", configPath)
err = api.InitializeDBServices()
if err != nil {
return errors.Wrap(err, "error initializing services")
}
return c.Command.Run(context, client)
}