Last active
February 7, 2023 20:30
-
-
Save mcharo/f4c1ad6f9ade211106e01d4a27e42136 to your computer and use it in GitHub Desktop.
Go CLI Playground
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
"os/exec" | |
"path/filepath" | |
"runtime" | |
"github.com/urfave/cli" | |
) | |
func main() { | |
app := cli.NewApp() | |
app.Name = "azdeploy" | |
app.Usage = "Deploy Azure template" | |
app.Version = "2019.5.1" | |
var powershell string | |
var lookupErr error | |
if runtime.GOOS == "windows" { | |
powershell, lookupErr = exec.LookPath("powershell") | |
if lookupErr != nil { | |
log.Fatal(lookupErr) | |
} | |
} else { | |
powershell, lookupErr = exec.LookPath("pwsh") | |
if lookupErr != nil { | |
log.Fatal(lookupErr) | |
} | |
} | |
var project, env string | |
app.Flags = []cli.Flag{ | |
cli.StringFlag{ | |
Name: "project, p", | |
Usage: "name of the project to deploy", | |
Destination: &project, | |
}, | |
cli.StringFlag{ | |
Name: "env, e", | |
Usage: "environment to deploy (e.g. qa, dev)", | |
Destination: &env, | |
}, | |
} | |
app.Action = func(c *cli.Context) error { | |
if project != "" && env != "" { | |
fmt.Println("Deploying", env, "environment for", project, "project") | |
scriptPath := filepath.Join(os.Getenv("TEMP"), "azdeploy.ps1") | |
writeScript(scriptPath) | |
cmd := exec.Command(powershell, "-NoProfile", "-File", scriptPath) | |
cmd.Stdout = os.Stdout | |
cmd.Stderr = os.Stderr | |
err := cmd.Run() | |
if err != nil { | |
log.Fatal(err) | |
} | |
} else { | |
cli.ShowAppHelpAndExit(c, 1) | |
} | |
return nil | |
} | |
err := app.Run(os.Args) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func writeScript(path string) { | |
script := []byte(` | |
"hello, world from $((Get-Process -Name azdeploy).Name)" | |
`) | |
err := ioutil.WriteFile(path, script, 0644) | |
if err != nil { | |
log.Fatal("Unable to generate deployment script") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why.... WHHHHHHYYYYYYYYY?!?!?!