-
Notifications
You must be signed in to change notification settings - Fork 130
Add bicep build managment #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var bicepCmd = &cobra.Command{ | ||
| Use: "bicep", | ||
| Short: "Manage bicep compiler", | ||
| Long: `Manage bicep compiler used by Radius`, | ||
| } | ||
|
|
||
| func init() { | ||
| rootCmd.AddCommand(bicepCmd) | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "github.com/Azure/radius/pkg/rad/bicep" | ||
| "github.com/Azure/radius/pkg/rad/logger" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var bicepCleanCmd = &cobra.Command{ | ||
| Use: "clean", | ||
| Short: "Clean installed bicep compiler", | ||
| Long: `Removes the local copy of the bicep compiler`, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| logger.LogInfo("removing local copy of bicep...") | ||
| ok, err := bicep.IsBicepInstalled() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if !ok { | ||
| logger.LogInfo("bicep is not installed") | ||
| return err | ||
| } | ||
|
|
||
| err = bicep.CleanBicep() | ||
| return err | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| bicepCmd.AddCommand(bicepCleanCmd) | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "github.com/Azure/radius/pkg/rad/bicep" | ||
| "github.com/Azure/radius/pkg/rad/logger" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var bicepDownloadCmd = &cobra.Command{ | ||
| Use: "download", | ||
| Short: "Download the bicep compiler", | ||
| Long: `Downloads the bicep compiler locally`, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| logger.LogInfo("downloading bicep...") | ||
| err := bicep.DownloadBicep() | ||
| return err | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| bicepCmd.AddCommand(bicepDownloadCmd) | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| package bicep | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "os" | ||
| "path" | ||
| "runtime" | ||
|
|
||
| "github.com/mitchellh/go-homedir" | ||
| ) | ||
|
|
||
| const radBicepEnvVar = "RAD_BICEP" | ||
| const downloadURIFmt = "https://radiuspublic.blob.core.windows.net/tools/bicep/edge/%s/%s" | ||
|
|
||
| // IsBicepInstalled returns true if our local copy of bicep is installed | ||
| func IsBicepInstalled() (bool, error) { | ||
| filepath, err := GetLocalBicepFilepath() | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| _, err = os.Stat(filepath) | ||
| if err != nil && os.IsNotExist(err) { | ||
|
rynowak marked this conversation as resolved.
|
||
| return false, nil | ||
| } else if err != nil { | ||
| return false, fmt.Errorf("error checking for %s: %v", filepath, err) | ||
| } | ||
|
|
||
| return true, nil | ||
| } | ||
|
|
||
| // CleanBicep cleans our local copy of bicep | ||
| func CleanBicep() error { | ||
| filepath, err := GetLocalBicepFilepath() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| err = os.Remove(filepath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to delete %s: %v", filepath, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // DownloadBicep updates our local copy of bicep | ||
| func DownloadBicep() error { | ||
| uri, err := getDownloadURI() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| resp, err := http.Get(uri) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to download bicep: %v", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| filepath, err := GetLocalBicepFilepath() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // create folders | ||
| err = os.MkdirAll(path.Dir(filepath), os.ModePerm) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create folder %s: %v", path.Dir(filepath), err) | ||
| } | ||
|
|
||
| // will truncate the file if it exists | ||
|
rynowak marked this conversation as resolved.
|
||
| out, err := os.Create(filepath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create file %s: %v", filepath, err) | ||
| } | ||
| defer out.Close() | ||
|
|
||
| // Write the body to file | ||
| _, err = io.Copy(out, resp.Body) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to write file %s: %v", filepath, err) | ||
| } | ||
|
|
||
| // get the filemode so we can mark it as executable | ||
| file, err := out.Stat() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read file attributes %s: %v", filepath, err) | ||
| } | ||
|
|
||
| // make file executable by everyone | ||
| err = out.Chmod(file.Mode() | 0111) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to change permissons for %s: %v", filepath, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // GetLocalBicepFilepath returns the local bicep file path. It does not verify that the file | ||
| // exists on disk. | ||
| func GetLocalBicepFilepath() (string, error) { | ||
| override, err := getBicepOverridePath() | ||
| if err != nil { | ||
| return "", err | ||
| } else if override != "" { | ||
| return override, nil | ||
| } | ||
|
|
||
| home, err := homedir.Dir() | ||
| if err != nil { | ||
| return "", fmt.Errorf("could not find home directory: %v", err) | ||
| } | ||
|
|
||
| filename, err := getBicepFilename() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return path.Join(home, ".rad", "bin", filename), nil | ||
| } | ||
|
|
||
| func getBicepFilename() (string, error) { | ||
| if runtime.GOOS == "darwin" { | ||
| return "rad-bicep", nil | ||
| } else if runtime.GOOS == "linux" { | ||
| return "rad-bicep", nil | ||
| } else if runtime.GOOS == "windows" { | ||
| return "rad-bicep.exe", nil | ||
| } else { | ||
| return "", fmt.Errorf("unsupported platform %s/%s", runtime.GOOS, runtime.GOARCH) | ||
| } | ||
| } | ||
|
|
||
| func getDownloadURI() (string, error) { | ||
| filename, err := getBicepFilename() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| if runtime.GOOS == "darwin" { | ||
| return fmt.Sprintf(downloadURIFmt, "macos-x64", filename), nil | ||
| } else if runtime.GOOS == "linux" { | ||
| return fmt.Sprintf(downloadURIFmt, "linux-x64", filename), nil | ||
| } else if runtime.GOOS == "windows" { | ||
| return fmt.Sprintf(downloadURIFmt, "windows-x64", filename), nil | ||
| } else { | ||
| return "", fmt.Errorf("unsupported platform %s/%s", runtime.GOOS, runtime.GOARCH) | ||
| } | ||
| } | ||
|
|
||
| func getBicepOverridePath() (string, error) { | ||
| override := os.Getenv(radBicepEnvVar) | ||
| if override == "" { | ||
| // not overridden | ||
| return "", nil | ||
| } | ||
|
|
||
| // Since is a development-only setting, we're cool with being noisy about it. | ||
| fmt.Println("") | ||
|
|
||
| file, err := os.Stat(override) | ||
| if err != nil { | ||
| return "", fmt.Errorf("cannot locate rad-bicep on overridden path %s: %v", override, err) | ||
| } | ||
|
|
||
| if !file.IsDir() { | ||
| // Since is a development-only setting, we're cool with being noisy about it. | ||
| fmt.Printf("rad bicep overridden to %s", override) | ||
| fmt.Println() | ||
| return override, nil | ||
| } | ||
|
|
||
| filename, err := getBicepFilename() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| override = path.Join(override, filename) | ||
| _, err = os.Stat(override) | ||
| if err != nil { | ||
| return "override", fmt.Errorf("cannot locate rad-bicep on overridden path %s: %v", override, err) | ||
| } | ||
|
|
||
| // Since is a development-only setting, we're cool with being noisy about it. | ||
| fmt.Printf("rad bicep overridden to %s", override) | ||
| fmt.Println() | ||
| return override, nil | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.