-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
160a3b4
commit 8da91d3
Showing
9 changed files
with
259 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package command | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"github.com/minamijoyo/tfmigrate/config" | ||
) | ||
|
||
// ApplyCommand is a command which computes a new state and pushes it to remote state. | ||
type ApplyCommand struct { | ||
Meta | ||
path string | ||
} | ||
|
||
// Run runs the procedure of this command. | ||
func (c *ApplyCommand) Run(args []string) int { | ||
cmdFlags := flag.NewFlagSet("apply", flag.ContinueOnError) | ||
if err := cmdFlags.Parse(args); err != nil { | ||
c.UI.Error(fmt.Sprintf("failed to parse arguments: %s", err)) | ||
return 1 | ||
} | ||
|
||
if len(cmdFlags.Args()) != 1 { | ||
c.UI.Error(fmt.Sprintf("The command expects 1 argument, but got %d", len(cmdFlags.Args()))) | ||
c.UI.Error(c.Help()) | ||
return 1 | ||
} | ||
|
||
c.Option = newOption() | ||
c.path = cmdFlags.Arg(0) | ||
|
||
source, err := ioutil.ReadFile(c.path) | ||
if err != nil { | ||
c.UI.Error(err.Error()) | ||
return 1 | ||
} | ||
|
||
config, err := config.ParseMigrationFile(c.path, source) | ||
if err != nil { | ||
c.UI.Error(err.Error()) | ||
return 1 | ||
} | ||
|
||
m, err := config.NewMigrator(c.Option) | ||
if err != nil { | ||
c.UI.Error(err.Error()) | ||
return 1 | ||
} | ||
|
||
err = m.Apply(context.Background()) | ||
if err != nil { | ||
c.UI.Error(err.Error()) | ||
return 1 | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
// Help returns long-form help text. | ||
func (c *ApplyCommand) Help() string { | ||
helpText := ` | ||
Usage: tfmigrate apply <PATH> | ||
Apply computes a new state and pushes it to remote state. | ||
It will fail if terraform plan detects any diffs with the new state. | ||
Arguments | ||
PATH A path of migration file | ||
` | ||
return strings.TrimSpace(helpText) | ||
} | ||
|
||
// Synopsis returns one-line help text. | ||
func (c *ApplyCommand) Synopsis() string { | ||
return "Computes a new state and pushes it to remote state" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package command | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/minamijoyo/tfmigrate/tfmigrate" | ||
"github.com/mitchellh/cli" | ||
) | ||
|
||
// Meta are the meta-options that are available on all or most commands. | ||
type Meta struct { | ||
// UI is a user interface representing input and output. | ||
UI cli.Ui | ||
|
||
// Option customizes a behaviror of Migrator. | ||
// It is used for shared settings across Migrator instances. | ||
Option *tfmigrate.MigratorOption | ||
} | ||
|
||
func newOption() *tfmigrate.MigratorOption { | ||
return &tfmigrate.MigratorOption{ | ||
ExecPath: os.Getenv("TFMIGRATE_EXEC_PATH"), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package command | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"github.com/minamijoyo/tfmigrate/config" | ||
) | ||
|
||
// PlanCommand is a command which computes a new state by applying state | ||
// migration operations to a temporary state. | ||
type PlanCommand struct { | ||
Meta | ||
path string | ||
} | ||
|
||
// Run runs the procedure of this command. | ||
func (c *PlanCommand) Run(args []string) int { | ||
cmdFlags := flag.NewFlagSet("plan", flag.ContinueOnError) | ||
if err := cmdFlags.Parse(args); err != nil { | ||
c.UI.Error(fmt.Sprintf("failed to parse arguments: %s", err)) | ||
return 1 | ||
} | ||
|
||
if len(cmdFlags.Args()) != 1 { | ||
c.UI.Error(fmt.Sprintf("The command expects 1 argument, but got %d", len(cmdFlags.Args()))) | ||
c.UI.Error(c.Help()) | ||
return 1 | ||
} | ||
|
||
c.Option = newOption() | ||
c.path = cmdFlags.Arg(0) | ||
|
||
source, err := ioutil.ReadFile(c.path) | ||
if err != nil { | ||
c.UI.Error(err.Error()) | ||
return 1 | ||
} | ||
|
||
config, err := config.ParseMigrationFile(c.path, source) | ||
if err != nil { | ||
c.UI.Error(err.Error()) | ||
return 1 | ||
} | ||
|
||
m, err := config.NewMigrator(c.Option) | ||
if err != nil { | ||
c.UI.Error(err.Error()) | ||
return 1 | ||
} | ||
|
||
err = m.Plan(context.Background()) | ||
if err != nil { | ||
c.UI.Error(err.Error()) | ||
return 1 | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
// Help returns long-form help text. | ||
func (c *PlanCommand) Help() string { | ||
helpText := ` | ||
Usage: tfmigrate plan <PATH> | ||
Plan computes a new state by applying state migration operations to a temporary state. | ||
It will fail if terraform plan detects any diffs with the new state. | ||
Arguments | ||
PATH A path of migration file | ||
` | ||
return strings.TrimSpace(helpText) | ||
} | ||
|
||
// Synopsis returns one-line help text. | ||
func (c *PlanCommand) Synopsis() string { | ||
return "Computes a new state" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters