Skip to content

Commit

Permalink
separate and rename files
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Feb 20, 2016
1 parent ac146b2 commit 58f1952
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 159 deletions.
4 changes: 2 additions & 2 deletions _tools/gen_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
)

func main() {
out, err := os.Create("commands.go")
out, err := os.Create("commands_gen.go")
if err != nil {
log.Fatal(err)
}

err = gen.Generate(out, "main.go", nil)
err = gen.Generate(out, "commands.go", nil)
if err != nil {
log.Fatal(err)
}
Expand Down
164 changes: 116 additions & 48 deletions commands.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,120 @@
// auto-generated file
//go:generate go run _tools/gen_commands.go

package main

import "github.com/motemen/go-cli"

func init() {
cli.Use(
&cli.Command{
Name: "",
Action: doMain,
Short: "mackerel-agent",
Long: "main process of mackerel-agent",
},
)

cli.Use(
&cli.Command{
Name: "version",
Action: doVersion,
Short: "display version of mackerel-agent",
Long: "version\n\ndisplay the version of mackerel-agent",
},
)

cli.Use(
&cli.Command{
Name: "configtest",
Action: doConfigtest,
Short: "configtest",
Long: "configtest\n\ndo configtest",
},
)

cli.Use(
&cli.Command{
Name: "retire",
Action: doRetire,
Short: "retire the host",
Long: "retire [-force]\n\nretire the host",
},
)

cli.Use(
&cli.Command{
Name: "once",
Action: doOnce,
Short: "output onetime",
Long: "once\n\noutput metrics and meta data of the host one time.\nThese data are only displayed and not posted to Mackerel.",
},
)
import (
"flag"
"fmt"
"os"
"runtime"

"github.com/Songmu/prompter"
"github.com/mackerelio/mackerel-agent/command"
"github.com/mackerelio/mackerel-agent/logging"
"github.com/mackerelio/mackerel-agent/mackerel"
"github.com/mackerelio/mackerel-agent/version"
"github.com/stf-storage/go-stf-server/config"
)

/* +command - mackerel-agent
main process of mackerel-agent
*/
func doMain(fs *flag.FlagSet, argv []string) error {
conf, err := resolveConfig(fs, argv)
if err != nil {
logger.Criticalf("faild to load config: %s", err)
return fmt.Errorf("failed to load config: %s", err)
}
if conf.Verbose {
logging.SetLogLevel(logging.DEBUG)
}
logger.Infof("Starting mackerel-agent version:%s, rev:%s, apibase:%s", version.VERSION, version.GITCOMMIT, conf.Apibase)

exitCode := start(conf)
if exitCode != exitStatusOK {
return fmt.Errorf("failed to exit normally. exitCode: %d", exitCode)
}
return nil
}

/* +command version - display version of mackerel-agent
version
display the version of mackerel-agent
*/
func doVersion(_ *flag.FlagSet, _ []string) error {
fmt.Printf("mackerel-agent version %s (rev %s) [%s %s %s] \n",
version.VERSION, version.GITCOMMIT, runtime.GOOS, runtime.GOARCH, runtime.Version())
return nil
}

/* +command configtest - configtest
configtest
do configtest
*/
func doConfigtest(fs *flag.FlagSet, argv []string) error {
conf, err := resolveConfig(fs, argv)
if err != nil {
logger.Criticalf("faild to test config: %s", err)
return fmt.Errorf("failed to test config: %s", err)
}
fmt.Fprintf(os.Stderr, "%s Syntax OK\n", conf.Conffile)
return nil
}

/* +command retire - retire the host
retire [-force]
retire the host
*/
func doRetire(fs *flag.FlagSet, argv []string) error {
conf, force := resolveConfigForRetire(fs, argv)

hostID, err := conf.LoadHostID()
if err != nil {
return fmt.Errorf("HostID file is not found")
}

api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, conf.Verbose)
if err != nil {
return fmt.Errorf("faild to create api client: %s", err)
}

if !force && !prompter.YN(fmt.Sprintf("retire this host? (hostID: %s)", hostID), false) {
return fmt.Errorf("Retirement is canceled.")
}

err = api.RetireHost(hostID)
if err != nil {
return fmt.Errorf("faild to retire the host: %s", err)
}
logger.Infof("This host (hostID: %s) has been retired.", hostID)
// just to try to remove hostID file.
err = conf.DeleteSavedHostID()
if err != nil {
logger.Warningf("Failed to remove HostID file: %s", err)
}
return nil
}

/* +command once - output onetime
once
output metrics and meta data of the host one time.
These data are only displayed and not posted to Mackerel.
*/
func doOnce(fs *flag.FlagSet, argv []string) error {
conf, err := resolveConfig(fs, argv)
if err != nil {
logger.Warningf("failed to load config (but `once` must not required conf): %s", err)
conf = &config.Config{}
}
command.RunOnce(conf)
return nil
}
52 changes: 52 additions & 0 deletions commands_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// auto-generated file

package main

import "github.com/motemen/go-cli"

func init() {
cli.Use(
&cli.Command{
Name: "",
Action: doMain,
Short: "mackerel-agent",
Long: "main process of mackerel-agent",
},
)

cli.Use(
&cli.Command{
Name: "version",
Action: doVersion,
Short: "display version of mackerel-agent",
Long: "version\n\ndisplay the version of mackerel-agent",
},
)

cli.Use(
&cli.Command{
Name: "configtest",
Action: doConfigtest,
Short: "configtest",
Long: "configtest\n\ndo configtest",
},
)

cli.Use(
&cli.Command{
Name: "retire",
Action: doRetire,
Short: "retire the host",
Long: "retire [-force]\n\nretire the host",
},
)

cli.Use(
&cli.Command{
Name: "once",
Action: doOnce,
Short: "output onetime",
Long: "once\n\noutput metrics and meta data of the host one time.\nThese data are only displayed and not posted to Mackerel.",
},
)
}
109 changes: 0 additions & 109 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:generate go run _tools/gen_commands.go

package main

import (
Expand All @@ -10,18 +8,14 @@ import (
"os/signal"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"syscall"
"time"

"github.com/Songmu/prompter"
"github.com/mackerelio/mackerel-agent/command"
"github.com/mackerelio/mackerel-agent/config"
"github.com/mackerelio/mackerel-agent/logging"
"github.com/mackerelio/mackerel-agent/mackerel"
"github.com/mackerelio/mackerel-agent/version"
"github.com/motemen/go-cli"
)

Expand Down Expand Up @@ -51,109 +45,6 @@ func main() {
cli.Run(os.Args[1:])
}

/* +command - mackerel-agent
main process of mackerel-agent
*/
func doMain(fs *flag.FlagSet, argv []string) error {
conf, err := resolveConfig(fs, argv)
if err != nil {
logger.Criticalf("faild to load config: %s", err)
return fmt.Errorf("failed to load config: %s", err)
}
if conf.Verbose {
logging.SetLogLevel(logging.DEBUG)
}
logger.Infof("Starting mackerel-agent version:%s, rev:%s, apibase:%s", version.VERSION, version.GITCOMMIT, conf.Apibase)

exitCode := start(conf)
if exitCode != exitStatusOK {
return fmt.Errorf("failed to exit normally. exitCode: %d", exitCode)
}
return nil
}

/* +command version - display version of mackerel-agent
version
display the version of mackerel-agent
*/
func doVersion(_ *flag.FlagSet, _ []string) error {
fmt.Printf("mackerel-agent version %s (rev %s) [%s %s %s] \n",
version.VERSION, version.GITCOMMIT, runtime.GOOS, runtime.GOARCH, runtime.Version())
return nil
}

/* +command configtest - configtest
configtest
do configtest
*/
func doConfigtest(fs *flag.FlagSet, argv []string) error {
conf, err := resolveConfig(fs, argv)
if err != nil {
logger.Criticalf("faild to test config: %s", err)
return fmt.Errorf("failed to test config: %s", err)
}
fmt.Fprintf(os.Stderr, "%s Syntax OK\n", conf.Conffile)
return nil
}

/* +command retire - retire the host
retire [-force]
retire the host
*/
func doRetire(fs *flag.FlagSet, argv []string) error {
conf, force := resolveConfigForRetire(fs, argv)

hostID, err := conf.LoadHostID()
if err != nil {
return fmt.Errorf("HostID file is not found")
}

api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, conf.Verbose)
if err != nil {
return fmt.Errorf("faild to create api client: %s", err)
}

if !force && !prompter.YN(fmt.Sprintf("retire this host? (hostID: %s)", hostID), false) {
return fmt.Errorf("Retirement is canceled.")
}

err = api.RetireHost(hostID)
if err != nil {
return fmt.Errorf("faild to retire the host: %s", err)
}
logger.Infof("This host (hostID: %s) has been retired.", hostID)
// just to try to remove hostID file.
err = conf.DeleteSavedHostID()
if err != nil {
logger.Warningf("Failed to remove HostID file: %s", err)
}
return nil
}

/* +command once - output onetime
once
output metrics and meta data of the host one time.
These data are only displayed and not posted to Mackerel.
*/
func doOnce(fs *flag.FlagSet, argv []string) error {
conf, err := resolveConfig(fs, argv)
if err != nil {
logger.Warningf("failed to load config (but `once` must not required conf): %s", err)
conf = &config.Config{}
}
command.RunOnce(conf)
return nil
}

func printRetireUsage() {
usage := fmt.Sprintf(`Usage of mackerel-agent retire:
-conf string
Expand Down

0 comments on commit 58f1952

Please sign in to comment.