Permalink
Browse files

Show package readme in browser

  • Loading branch information...
FabianKramm committed Sep 25, 2018
1 parent 3a036dc commit 241b77df37e5ae9a35c42fae94451d160939af1c
Showing with 251 additions and 44 deletions.
  1. +12 −3 Gopkg.lock
  2. +62 −3 cmd/add.go
  3. +72 −37 cmd/remove.go
  4. +0 −1 main.go
  5. +105 −0 pkg/util/tar/tar.go
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -8,13 +8,19 @@ import (
"strconv"
"strings"
"k8s.io/helm/pkg/repo"
"github.com/covexo/devspace/pkg/util/stdinutil"
"github.com/covexo/devspace/pkg/util/tar"
"github.com/covexo/devspace/pkg/util/yamlutil"
helmClient "github.com/covexo/devspace/pkg/devspace/clients/helm"
"github.com/covexo/devspace/pkg/devspace/clients/kubectl"
"github.com/covexo/devspace/pkg/devspace/config/configutil"
"github.com/covexo/devspace/pkg/devspace/config/v1"
"github.com/covexo/devspace/pkg/util/log"
"github.com/russross/blackfriday"
"github.com/skratchdot/open-golang/open"
"github.com/spf13/cobra"
)
@@ -47,6 +53,7 @@ type addPortCmdFlags struct {
type addPackageFlags struct {
AppVersion string
ChartVersion string
SkipQuestion bool
}
func init() {
@@ -148,6 +155,7 @@ func init() {
addPackageCmd.Flags().StringVar(&cmd.packageFlags.AppVersion, "app-version", "", "App version")
addPackageCmd.Flags().StringVar(&cmd.packageFlags.ChartVersion, "chart-version", "", "Chart version")
addPackageCmd.Flags().BoolVar(&cmd.packageFlags.SkipQuestion, "skip-question", false, "Skips the question to show the readme in a browser")
addCmd.AddCommand(addPackageCmd)
}
@@ -185,7 +193,6 @@ func (cmd *AddCmd) RunAddPackage(cobraCmd *cobra.Command, args []string) {
}
requirementsFile := filepath.Join(cwd, "chart", "requirements.yaml")
_, err = os.Stat(requirementsFile)
if os.IsNotExist(err) {
entry := "dependencies:\n" +
@@ -233,7 +240,60 @@ func (cmd *AddCmd) RunAddPackage(cobraCmd *cobra.Command, args []string) {
log.Fatal(err)
}
log.Donef("Successfully added %s as chart dependency", version.GetName())
f, err := os.OpenFile(filepath.Join(cwd, "chart", "values.yaml"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
log.Fatal(err)
}
defer f.Close()
if _, err = f.WriteString("\n" + version.GetName() + ": {}\n"); err != nil {
log.Fatal(err)
}
log.Donef("Successfully added %s as chart dependency, you can configure the package in 'chart/values.yaml'", version.GetName())
cmd.showReadme(version)
}
func (cmd *AddCmd) showReadme(chartVersion *repo.ChartVersion) {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
if cmd.packageFlags.SkipQuestion {
return
}
showReadme := *stdinutil.GetFromStdin(&stdinutil.GetFromStdinParams{
Question: "Do you want to open the package README? (y|n)",
DefaultValue: "y",
ValidationRegexPattern: "^(y|n)",
})
if showReadme == "n" {
return
}
content, err := tar.ExtractSingleFileToStringTarGz(filepath.Join(cwd, "chart", "charts", chartVersion.GetName()+"-"+chartVersion.GetVersion()+".tgz"), chartVersion.GetName()+"/README.md")
if err != nil {
log.Fatal(err)
}
output := blackfriday.MarkdownCommon([]byte(content))
f, err := os.OpenFile(filepath.Join(os.TempDir(), "Readme.html"), os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
log.Fatal(err)
}
defer f.Close()
_, err = f.Write(output)
if err != nil {
log.Fatal(err)
}
f.Close()
open.Start(f.Name())
}
// RunAddSync executes the add sync command logic
@@ -284,7 +344,6 @@ func (cmd *AddCmd) RunAddSync(cobraCmd *cobra.Command, args []string) {
config.DevSpace.Sync = &syncConfig
err = configutil.SaveConfig()
if err != nil {
log.Fatalf("Couldn't save config file: %s", err.Error())
}
View
@@ -17,9 +17,10 @@ import (
// RemoveCmd holds the information needed for the remove command
type RemoveCmd struct {
syncFlags *removeSyncCmdFlags
portFlags *removePortCmdFlags
workdir string
syncFlags *removeSyncCmdFlags
portFlags *removePortCmdFlags
packageFlags *removePackageCmdFlags
workdir string
}
type removeSyncCmdFlags struct {
@@ -34,10 +35,15 @@ type removePortCmdFlags struct {
RemoveAll bool
}
type removePackageCmdFlags struct {
RemoveAll bool
}
func init() {
cmd := &RemoveCmd{
syncFlags: &removeSyncCmdFlags{},
portFlags: &removePortCmdFlags{},
syncFlags: &removeSyncCmdFlags{},
portFlags: &removePortCmdFlags{},
packageFlags: &removePackageCmdFlags{},
}
removeCmd := &cobra.Command{
@@ -123,6 +129,7 @@ func init() {
Run: cmd.RunRemovePackage,
}
removePackageCmd.Flags().BoolVar(&cmd.packageFlags.RemoveAll, "all", false, "Remove all packages")
removeCmd.AddCommand(removePackageCmd)
}
@@ -133,6 +140,10 @@ func (cmd *RemoveCmd) RunRemovePackage(cobraCmd *cobra.Command, args []string) {
log.Fatal(err)
}
if len(args) == 0 && cmd.packageFlags.RemoveAll == false {
log.Fatal("You need to specify a package name or the --all flag")
}
requirementsPath := filepath.Join(cwd, "chart", "requirements.yaml")
yamlContents := map[interface{}]interface{}{}
@@ -147,48 +158,72 @@ func (cmd *RemoveCmd) RunRemovePackage(cobraCmd *cobra.Command, args []string) {
log.Fatalf("Error parsing yaml: %v", dependencies)
}
for key, dependency := range dependenciesArr {
dependencyMap, ok := dependency.(map[interface{}]interface{})
if ok == false {
log.Fatalf("Error parsing yaml: %v", dependencies)
}
if cmd.packageFlags.RemoveAll == false {
for key, dependency := range dependenciesArr {
dependencyMap, ok := dependency.(map[interface{}]interface{})
if ok == false {
log.Fatalf("Error parsing yaml: %v", dependencies)
}
if name, ok := dependencyMap["name"]; ok {
if name == args[0] {
dependenciesArr = append(dependenciesArr[:key], dependenciesArr[key+1:]...)
yamlContents["dependencies"] = dependenciesArr
if name, ok := dependencyMap["name"]; ok {
if name == args[0] {
dependenciesArr = append(dependenciesArr[:key], dependenciesArr[key+1:]...)
yamlContents["dependencies"] = dependenciesArr
err = yamlutil.WriteYamlToFile(yamlContents, requirementsPath)
if err != nil {
log.Fatal(err)
}
err = yamlutil.WriteYamlToFile(yamlContents, requirementsPath)
if err != nil {
log.Fatal(err)
}
// Rebuild dependencies
kubectl, err := kubectl.NewClient()
if err != nil {
log.Fatalf("Unable to create new kubectl client: %v", err)
cmd.rebuildDependencies()
break
}
}
}
helm, err := helmClient.NewClient(kubectl, false)
if err != nil {
log.Fatalf("Error initializing helm client: %v", err)
}
log.StartWait("Update chart dependencies")
err = helm.UpdateDependencies(filepath.Join(cwd, "chart"))
log.StopWait()
if err != nil {
log.Fatal(err)
}
log.Donef("Successfully removed dependency %s", args[0])
return
} else {
yamlContents["dependencies"] = []interface{}{}
break
}
err = yamlutil.WriteYamlToFile(yamlContents, requirementsPath)
if err != nil {
log.Fatal(err)
}
cmd.rebuildDependencies()
log.Done("Successfully removed all dependencies")
return
}
}
log.Donef("Successfully removed dependency %s", args[0])
log.Done("No dependencies found")
}
func (cmd *RemoveCmd) rebuildDependencies() {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
// Rebuild dependencies
kubectl, err := kubectl.NewClient()
if err != nil {
log.Fatalf("Unable to create new kubectl client: %v", err)
}
helm, err := helmClient.NewClient(kubectl, false)
if err != nil {
log.Fatalf("Error initializing helm client: %v", err)
}
log.StartWait("Update chart dependencies")
err = helm.UpdateDependencies(filepath.Join(cwd, "chart"))
log.StopWait()
if err != nil {
log.Fatal(err)
}
}
// RunRemoveSync executes the remove sync command logic
View
@@ -5,7 +5,6 @@ import (
"github.com/covexo/devspace/cmd"
"github.com/covexo/devspace/pkg/devspace/upgrade"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
Oops, something went wrong.

0 comments on commit 241b77d

Please sign in to comment.