-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use kiota list command to install dependencies (#33)
* Use kiota list command to install dependencies instead of hard-coded list * Remove unnecessary log
- Loading branch information
1 parent
ee8039d
commit f343306
Showing
1 changed file
with
32 additions
and
11 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package main | |
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/fs" | ||
"log" | ||
|
@@ -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 | ||
|