Last active
September 27, 2019 16:40
-
-
Save stavxyz/b6498f4cea50b915bc767f2400b2a8a3 to your computer and use it in GitHub Desktop.
read a gatsby config with golang
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
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
"os" | |
"os/exec" | |
) | |
func main() { | |
fmt.Printf("Running node command.") | |
out, err := exec.Command( | |
"node", "-e", "process.stdout.write(JSON.stringify(require(\"./gatsby-config.js\")))", | |
).Output() | |
if err != nil { | |
fmt.Printf("Node command failed.") | |
log.Fatal(err) | |
} | |
fmt.Printf("Node command succeeded.\n") | |
gatsbyConfig := make(map[string]interface{}) | |
fmt.Printf("Decoding json.\n") | |
err = json.Unmarshal(out, &gatsbyConfig) | |
fmt.Printf("conf --> %+v \n", gatsbyConfig) | |
if err != nil { | |
fmt.Printf("Failed to decode json.\n") | |
log.Fatal(err) | |
} | |
fmt.Printf("JSON decoding succeeded.\n") | |
b, err := json.MarshalIndent(gatsbyConfig, "", " ") | |
os.Stdout.Write(b) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment