-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from patheticGeek/feat/config-file
✨ Able to read `prjkt.yaml` and do actions from it
- Loading branch information
Showing
14 changed files
with
336 additions
and
11 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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
node_modules/ | ||
dist/ | ||
# For testing | ||
new-prjkt/ | ||
test.yaml |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package types | ||
|
||
type Action map[string]string | ||
|
||
type ActionsMap []Action | ||
|
||
type PrjktYAML struct { | ||
Welcome_message string | ||
Error_message string | ||
Success_message string | ||
Actions ActionsMap | ||
} |
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,98 @@ | ||
package utils | ||
|
||
import ( | ||
types "internal/types" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/AlecAivazis/survey/v2" | ||
) | ||
|
||
func ExecAction(action types.Action, destination string) error { | ||
err := Shellout(action["run"]) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func ExecOptionAction(action types.Action, destination string) error { | ||
options := []string{} | ||
for _, option := range strings.Split(action["options"], ",") { | ||
options = append(options, strings.Trim(option, " ")) | ||
} | ||
|
||
selectedOption := "" | ||
prompt := &survey.Select{ | ||
Message: action["prompt"], | ||
Options: options, | ||
} | ||
survey.AskOne(prompt, &selectedOption) | ||
|
||
if action["option-"+selectedOption] == "" { | ||
return nil | ||
} | ||
|
||
err := Shellout(action["option-"+selectedOption]) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func ReplaceAction(action types.Action, destination string) error { | ||
answer := "" | ||
prompt := &survey.Input{ | ||
Message: action["prompt"], | ||
} | ||
survey.AskOne(prompt, &answer) | ||
|
||
filePatterns := strings.Split(action["files"], ",") | ||
for _, filePattern := range filePatterns { | ||
matchingFiles, err := filepath.Glob(strings.Trim(filePattern, " ")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, file := range matchingFiles { | ||
read, err := ioutil.ReadFile(file) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
newContents := strings.Replace(string(read), action["to_replace"], answer, -1) | ||
|
||
err = ioutil.WriteFile(file, []byte(newContents), 0) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func DeleteAction(action types.Action, destination string) error { | ||
filePatterns := strings.Split(action["files"], ",") | ||
|
||
for _, filePattern := range filePatterns { | ||
filesToDelete, err := filepath.Glob(strings.Trim(filePattern, " ")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, file := range filesToDelete { | ||
if err := os.Remove(file); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
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
Oops, something went wrong.