Skip to content
This repository was archived by the owner on Jan 25, 2022. It is now read-only.

Commit df1f9d8

Browse files
committed
Refactor helpers to package.
1 parent 961ed40 commit df1f9d8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+399
-8126
lines changed

.idea/workspace.xml

+145-39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/build.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"github.com/newclarity/JsonToConfig/jtc"
6+
"github.com/wplib/deploywp/deploywp"
7+
"github.com/newclarity/JsonToConfig/ux"
8+
)
9+
10+
11+
func init() {
12+
rootCmd.AddCommand(buildCmd)
13+
}
14+
15+
16+
var buildCmd = &cobra.Command{
17+
Use: jtc.CmdBuild,
18+
Short: ux.SprintfBlue("Build a Pantheon website."),
19+
Long: ux.SprintfBlue("Build a Pantheon website."),
20+
Run: Build,
21+
}
22+
func Build(cmd *cobra.Command, args []string) {
23+
for range OnlyOnce {
24+
tmpl := ProcessArgs(cmd, args)
25+
Cmd.State = tmpl.State
26+
if Cmd.State.IsNotOk() {
27+
Cmd.State.PrintResponse()
28+
break
29+
}
30+
31+
tmpl.LoadHelpers(deploywp.GetHelpers)
32+
33+
Cmd.State = tmpl.Load()
34+
if Cmd.State.IsNotOk() {
35+
Cmd.State.PrintResponse()
36+
break
37+
}
38+
39+
ux.PrintflnOk("Running build.")
40+
Cmd.State = tmpl.Run()
41+
Cmd.State.PrintResponse()
42+
43+
//dwp := deploywp.TypeDeployWp{}
44+
//dwp := deploywp.HelperLoadDeployWp(tmpl.JsonStruct.Json, tmpl.Exec.GetArgs()...)
45+
//if dwp.State.IsNotOk() {
46+
// dwp.State.PrintResponse()
47+
// break
48+
//}
49+
//
50+
//dwp.Exec = tmpl.JsonStruct.Exec
51+
//Cmd.State = dwp.Run()
52+
//
53+
//if Cmd.State.IsNotOk() {
54+
// Cmd.State.SetExitCode(1)
55+
// //Cmd.State.Exit(1)
56+
// break
57+
//}
58+
//
59+
//fmt.Printf("\n%s\nFINISHED\n", Cmd.State.SprintResponse())
60+
}
61+
}

cmd/const.go

-27
This file was deleted.

cmd/funcs.go

+20-86
Original file line numberDiff line numberDiff line change
@@ -2,110 +2,44 @@ package cmd
22

33
import (
44
"github.com/spf13/cobra"
5-
"github.com/wplib/deploywp/jtc"
6-
"strings"
5+
"github.com/newclarity/JsonToConfig/jtc"
6+
"path/filepath"
77
)
88

99

1010
func ProcessArgs(cmd *cobra.Command, args []string) *jtc.ArgTemplate {
11-
tmpl := jtc.NewArgTemplate()
11+
var tmpl *jtc.ArgTemplate
12+
// tmpl := jtc.NewArgTemplate()
1213

1314
for range OnlyOnce {
14-
var err error
15+
tmpl = Cmd
1516

1617
_ = tmpl.Exec.SetArgs(cmd.Use)
1718
_ = tmpl.Exec.AddArgs(args...)
1819

19-
fl := cmd.Flags()
20-
21-
// flagConfigFile = "config"
22-
// flagVersion = "version"
23-
24-
25-
// Dry run mode.
26-
Cmd.DryRun, err = fl.GetBool(flagDryRun)
27-
if err != nil {
28-
tmpl.OverWrite = false
29-
tmpl.RemoveFiles = false
30-
}
31-
if Cmd.DryRun {
32-
tmpl.OverWrite = false
33-
tmpl.RemoveFiles = false
34-
} else {
35-
tmpl.OverWrite = true
36-
tmpl.RemoveFiles = true
37-
}
38-
39-
40-
// Json file.
41-
Cmd.JsonFile, err = fl.GetString(flagJsonFile)
42-
if err != nil {
43-
Cmd.JsonFile = defaultJsonFile
44-
}
45-
if Cmd.JsonFile == "" {
46-
Cmd.JsonFile = defaultJsonFile
47-
}
48-
tmpl.State = tmpl.SetJsonFile(Cmd.JsonFile)
49-
if tmpl.State.IsNotOk() {
50-
tmpl.State.SetError("ERROR: %s", err)
51-
break
52-
}
53-
Cmd.JsonFile = tmpl.JsonFile.GetPath()
54-
55-
56-
// Template file.
57-
for range OnlyOnce {
58-
Cmd.TemplateFile, err = fl.GetString(flagTemplateFile)
59-
if err != nil {
60-
Cmd.TemplateFile = defaultTemplateFile
61-
}
62-
if Cmd.TemplateFile == "" {
63-
Cmd.TemplateFile = defaultTemplateFile
64-
}
65-
66-
tmpl.State = tmpl.SetTemplateFile(Cmd.TemplateFile)
67-
if tmpl.State.IsOk() {
68-
break
20+
ext := ""
21+
if len(args) >= 1 {
22+
ext := filepath.Ext(args[0])
23+
if ext == ".json" {
24+
tmpl.Json.Name = args[0]
25+
} else if ext == ".tmpl" {
26+
tmpl.Template.Name = args[0]
6927
}
28+
}
7029

71-
// Try again based on the json file.
72-
Cmd.TemplateFile = strings.TrimSuffix(tmpl.JsonFile.GetPath(), defaultJsonFileSuffix) + defaultTemplateFileSuffix
73-
tmpl.State = tmpl.SetTemplateFile(Cmd.TemplateFile)
74-
if tmpl.State.IsNotOk() {
75-
tmpl.State.SetError("ERROR: %s", err)
76-
break
30+
if len(args) >= 2 {
31+
ext = filepath.Ext(args[1])
32+
if ext == ".json" {
33+
tmpl.Json.Name = args[1]
34+
} else if ext == ".tmpl" {
35+
tmpl.Template.Name = args[1]
7736
}
7837
}
7938

80-
81-
// Output file.
82-
Cmd.OutFile, err = fl.GetString(flagOutputFile)
83-
if err != nil {
84-
Cmd.OutFile = ""
85-
}
86-
if Cmd.OutFile == defaultOutFile {
87-
tmpl.OutFile = nil
88-
}
89-
tmpl.State = tmpl.SetOutFile(Cmd.OutFile)
39+
tmpl.ValidateArgs()
9040
if tmpl.State.IsNotOk() {
91-
tmpl.State.SetError("ERROR: %s", err)
9241
break
9342
}
94-
95-
96-
// Chdir.
97-
Cmd.Chdir, err = fl.GetBool(flagChdir)
98-
if Cmd.Chdir {
99-
tmpl.State = tmpl.JsonFile.Chdir()
100-
if tmpl.State.IsNotOk() {
101-
//tmpl.State.SetError("ERROR: %s", err)
102-
break
103-
}
104-
}
105-
106-
107-
tmpl.SetValid()
108-
tmpl.State.SetOk("Processed arguments.")
10943
}
11044

11145
return tmpl

cmd/helpers.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package cmd
22

33
import (
44
"github.com/spf13/cobra"
5-
"github.com/wplib/deploywp/ux"
5+
"github.com/newclarity/JsonToConfig/jtc"
6+
"github.com/newclarity/JsonToConfig/ux"
67
)
78

89

@@ -12,19 +13,17 @@ func init() {
1213

1314

1415
var helpersCmd = &cobra.Command{
15-
Use: cmdHelpers,
16+
Use: jtc.CmdHelpers,
1617
Short: ux.SprintfBlue("Show all built-in template helpers."),
17-
Long: ux.SprintfBlue(`...`),
18-
Run: Helpers,
18+
Long: ux.SprintfBlue("Show all built-in template helpers."),
19+
Run: Helpers,
1920
}
2021
func Helpers(cmd *cobra.Command, args []string) {
2122
for range OnlyOnce {
22-
//Cmd.State = ux.NewState(Cmd.Debug)
23-
//var tmpl *jtc.ArgTemplate
24-
2523
tmpl := ProcessArgs(rootCmd, args)
2624
// Ignore errors as there's no args.
2725

2826
tmpl.PrintHelpers()
27+
Cmd.State.Clear()
2928
}
30-
}
29+
}

cmd/load.go

+12-29
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,41 @@ package cmd
22

33
import (
44
"github.com/spf13/cobra"
5-
"github.com/wplib/deploywp/ux"
6-
"strings"
5+
"github.com/newclarity/JsonToConfig/jtc"
6+
"github.com/newclarity/JsonToConfig/ux"
77
)
88

99

1010
func init() {
1111
rootCmd.AddCommand(loadCmd)
1212
}
1313

14+
1415
// releaseCmd represents the release command
1516
var loadCmd = &cobra.Command{
16-
Use: cmdLoad,
17+
Use: jtc.CmdLoad,
1718
Short: ux.SprintfBlue("Load and execute a template file."),
18-
Long: ux.SprintfBlue(`...`),
19+
Long: ux.SprintfBlue("Load and execute a template file."),
1920
Run: LoadTemplate,
21+
DisableFlagParsing: false,
2022
}
2123
func LoadTemplate(cmd *cobra.Command, args []string) {
2224
for range OnlyOnce {
23-
//Cmd.State = ux.NewState(Cmd.Debug)
24-
//var tmpl *jtc.ArgTemplate
25-
26-
/*
27-
Allow this to be used as a UNIX script.
28-
The following should be placed on the first line.
29-
#!/usr/bin/env deploywp load
30-
*/
31-
if len(args) > 0 {
32-
t := args[0]
33-
args = args[1:]
34-
_ = cmd.Flags().Set(flagJsonFile, t)
35-
36-
t = strings.TrimSuffix(t, "json") + "tmpl"
37-
_ = cmd.Flags().Set(flagTemplateFile, t)
38-
}
39-
40-
tmpl := ProcessArgs(rootCmd, args)
25+
tmpl := ProcessArgs(cmd, args)
4126
Cmd.State = tmpl.State
4227
if Cmd.State.IsNotOk() {
4328
Cmd.State.PrintResponse()
4429
break
4530
}
4631

47-
Cmd.State = tmpl.LoadTemplate()
32+
Cmd.State = tmpl.Load()
4833
if Cmd.State.IsNotOk() {
4934
Cmd.State.PrintResponse()
5035
break
5136
}
5237

53-
Cmd.State = tmpl.RunTemplate()
54-
if Cmd.State.IsNotOk() {
55-
Cmd.State.PrintResponse()
56-
break
57-
}
38+
ux.PrintflnOk("Loading template '%s' and saving result to '%s'", tmpl.Template.Name, tmpl.Output.Name)
39+
Cmd.State = tmpl.Run()
40+
Cmd.State.PrintResponse()
5841
}
59-
}
42+
}

0 commit comments

Comments
 (0)