github.com/maruel/nin@v0.0.0-20220112143044-f35891e3ce7e/cmd/nin/browse.go (about) 1 // Copyright 2011 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "os" 19 "os/exec" 20 "strings" 21 22 "github.com/maruel/nin" 23 ) 24 25 // TODO(maruel): Rewrite as a native Go server anyway, no need to depend on 26 // python. 27 const browsePy = "abc" 28 29 // Run in "browse" mode, which execs a Python webserver. 30 // \a ninjaCommand is the command used to invoke ninja. 31 // \a args are the number of arguments to be passed to the Python script. 32 // \a argv are arguments to be passed to the Python script. 33 // This function does not return if it runs successfully. 34 func runBrowsePython(state *nin.State, ninjaCommand string, inputFile string, args []string) { 35 // The original C++ code exec() python as the parent, which is super weird. 36 // We cannot do this easily so do it the normal way for now. 37 38 cmd := exec.Command("python3", "-", "--ninja-command", ninjaCommand, "-f", "input_file") 39 cmd.Args = append(cmd.Args, args...) 40 cmd.Stderr = os.Stderr 41 cmd.Stdout = os.Stdout 42 cmd.Stdin = strings.NewReader(browsePy) 43 if err := cmd.Run(); err != nil { 44 if exitErr, ok := err.(*exec.ExitError); ok { 45 os.Exit(exitErr.ExitCode()) 46 } 47 } 48 os.Exit(0) 49 }