Skip to content

Commit

Permalink
Use kiota list command to install dependencies (#33)
Browse files Browse the repository at this point in the history
* Use kiota list command to install dependencies instead of hard-coded list

* Remove unnecessary log
  • Loading branch information
kfcampbell authored Nov 9, 2023
1 parent ee8039d commit f343306
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions post-processors/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"encoding/json"
"fmt"
"io/fs"
"log"
Expand Down Expand Up @@ -64,33 +65,53 @@ func run() error {

err = cmd.Run()

stdoutOutput := stdout.String()
_ = stdout.String()
stderrOutput := stderr.String()

if err != nil {
return fmt.Errorf("could not initialize Go module: %v\nfull error log:\n%s", err, stderrOutput)
}

log.Printf("output of module initialization: %v", stdoutOutput)
cmd = exec.Command("kiota", "info", "-l", "Go", "--json")
cmd.Dir = dirPath

stdout.Reset()
stderr.Reset()

output, err := cmd.Output()
if err != nil {
fmt.Printf("could not run kiota info: %v\nfull error log:\n%s", err, stderr.String())
}

// parse the json returned by kiota info, extract the "dependencies" field,
// and construct a "go get" command for each with the "name" and "version" subfields used.
// this code could result in exceptions if the format of the command isn't changed.
// if/when we know for sure that it's stabilized, this can be refactored to use structs
// and not all of these interface{}s and sleazy casting
var infoResult map[string]interface{}
if err := json.Unmarshal(output, &infoResult); err != nil {
return fmt.Errorf("could not parse kiota info output: %v", err)
}
deps := infoResult["dependencies"].([]interface{})
depsToInstall := make([]string, len(deps))
for i, d := range deps {
dep := d.(map[string]interface{})
name := dep["name"].(string)
version := dep["version"].(string)

deps := [7]string{
"github.com/microsoft/[email protected]",
"github.com/microsoft/[email protected]",
"github.com/microsoft/[email protected]",
"github.com/microsoft/[email protected]",
"github.com/microsoft/[email protected]",
"github.com/microsoft/[email protected]",
"github.com/microsoft/[email protected]",
fullDep := fmt.Sprintf("%s@%s", name, version)
depsToInstall[i] = fullDep
}

for _, dep := range deps {
for _, dep := range depsToInstall {
cmd = exec.Command("go", "get", dep)
cmd.Dir = dirPath

_, err := cmd.Output()
if err != nil {
return fmt.Errorf("could not get dependencies: %v", err)
}
fmt.Printf("installed dependency %s\n", dep)
}

return nil
Expand Down

0 comments on commit f343306

Please sign in to comment.