@@ -2,6 +2,9 @@ package main
22
33import (
44 "fmt"
5+ "os/exec"
6+ "io"
7+ "strings"
58 "os"
69
710 "github.com/brendandburns/configula/pkg/configula"
@@ -11,15 +14,43 @@ import (
1114var (
1215 pythonCommand = flag .String ("python" , "python3" , "The executable to run for Python, overridden by $CONFIGULA_PYTHON" )
1316 dryRun = flag .Bool ("debug" , false , "If true, only output the interim program, don't execute" )
17+ file = flag .StringP ("filename" , "f" , "" , "The file name to process" )
1418)
1519
20+ func pluginMain () {
21+ if len (* file ) == 0 || len (flag .Args ()) != 1 {
22+ fmt .Fprintf (os .Stderr , "Usage: kubectl configula create|apply|delete -f <some-file>\n " )
23+ return
24+ }
25+ cmd := exec .Command ("kubectl" , flag .Args ()[0 ], "-f" , "-" )
26+ output , err := cmd .StdinPipe ()
27+ if err != nil {
28+ panic (err )
29+ }
30+ go func () {
31+ process (* file , output )
32+ if err := output .Close (); err != nil {
33+ panic (err )
34+ }
35+ }()
36+ cmd .Run ()
37+ }
38+
1639func main () {
1740 flag .Parse ()
41+ if strings .HasSuffix (os .Args [0 ], "kubectl-configula" ) {
42+ pluginMain ()
43+ return
44+ }
1845 if len (flag .Args ()) == 0 {
1946 fmt .Fprintf (os .Stderr , "Usage: configula [--debug] [--python=/some/path] <path/to/config/file>\n " )
2047 os .Exit (- 1 )
2148 }
22- file , err := os .Open (flag .Args ()[0 ])
49+ process (flag .Args ()[0 ], os .Stdout )
50+ }
51+
52+ func process (filename string , output io.Writer ) {
53+ file , err := os .Open (filename )
2354 if err != nil {
2455 fmt .Fprintf (os .Stderr , "Failed to read %s: %s" , flag .Args ()[0 ], err .Error ())
2556 os .Exit (1 )
@@ -38,7 +69,7 @@ func main() {
3869 configula .NewPythonExecutor (pythonExec ),
3970 }
4071
41- if err := rn .Run (file , os . Stdout , * dryRun ); err != nil {
72+ if err := rn .Run (file , output , * dryRun ); err != nil {
4273 fmt .Fprintf (os .Stderr , "Failed to execute: %s" , err )
4374 os .Exit (- 1 )
4475 }
0 commit comments