Skip to content
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

chore: make SetPluginDir function more clear #1206

Merged
merged 1 commit into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/devstream/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"github.com/devstream-io/devstream/cmd/devstream/list"
"github.com/devstream-io/devstream/internal/pkg/completion"
"github.com/devstream-io/devstream/internal/pkg/configmanager"
"github.com/devstream-io/devstream/internal/pkg/pluginengine"
"github.com/devstream-io/devstream/internal/pkg/pluginmanager"
"github.com/devstream-io/devstream/internal/pkg/version"
"github.com/devstream-io/devstream/pkg/util/file"
"github.com/devstream-io/devstream/pkg/util/log"
)

Expand Down Expand Up @@ -71,7 +71,7 @@ func GetPluginsAndPluginDirFromConfig() (tools []configmanager.Tool, pluginDir s
}

// combine plugin dir from config file and flag
if err := file.SetPluginDir(cfg.PluginDir); err != nil {
if err = pluginengine.SetPluginDir(cfg.PluginDir); err != nil {
return nil, "", err
}

Expand Down Expand Up @@ -116,7 +116,7 @@ func GetPluginsAndPluginDirFromFlags() (tools []configmanager.Tool, pluginDir st
}

// 2. handle plugin dir
if err := file.SetPluginDir(""); err != nil {
if err = pluginengine.SetPluginDir(""); err != nil {
return nil, "", err
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/devstream/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var (

var listCMD = &cobra.Command{
Use: "list",
Short: "This command only supports listing plugins now.",
Short: "This command only supports listing plugins now",
}

var listPluginsCMD = &cobra.Command{
Expand All @@ -27,7 +27,7 @@ Examples:
Run: listPluginsCMDFunc,
}

func listPluginsCMDFunc(cmd *cobra.Command, args []string) {
func listPluginsCMDFunc(_ *cobra.Command, _ []string) {
list.List(pluginFilter)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/devstream/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var template string

var showCMD = &cobra.Command{
Use: "show",
Short: "Show is used to print plugins' configuration templates or status.",
Short: "Show is used to print plugins' configuration templates or status",
}

var showConfigCMD = &cobra.Command{
Expand Down
2 changes: 2 additions & 0 deletions internal/pkg/configmanager/configmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var (

// Config records rendered config values and is used as a general config in DevStream.
type Config struct {
// Command line flag have a higher priority than the config file.
// If you used the `--plugin-dir` flag with `dtm`, then the "pluginDir" in the config file will be ignored.
PluginDir string
Tools []Tool `yaml:"tools"`
State *State
Expand Down
5 changes: 3 additions & 2 deletions internal/pkg/pluginengine/cmd_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import (
"github.com/devstream-io/devstream/internal/pkg/configmanager"
"github.com/devstream-io/devstream/internal/pkg/pluginmanager"
"github.com/devstream-io/devstream/internal/pkg/statemanager"
"github.com/devstream-io/devstream/pkg/util/file"
"github.com/devstream-io/devstream/pkg/util/interact"
"github.com/devstream-io/devstream/pkg/util/log"
)

const askUserIfContinue string = "Continue? [y/n]"

func Apply(configFile string, continueDirectly bool) error {
cfg, err := configmanager.NewManager(configFile).LoadConfig()
if err != nil {
return err
}

if err := file.SetPluginDir(cfg.PluginDir); err != nil {
if err = SetPluginDir(cfg.PluginDir); err != nil {
log.Errorf("Error: %s.", err)
}

Expand Down
3 changes: 1 addition & 2 deletions internal/pkg/pluginengine/cmd_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/devstream-io/devstream/internal/pkg/configmanager"
"github.com/devstream-io/devstream/internal/pkg/pluginmanager"
"github.com/devstream-io/devstream/internal/pkg/statemanager"
"github.com/devstream-io/devstream/pkg/util/file"
"github.com/devstream-io/devstream/pkg/util/interact"
"github.com/devstream-io/devstream/pkg/util/log"
)
Expand All @@ -18,7 +17,7 @@ func Remove(configFile string, continueDirectly bool, isForceDelete bool) error
return err
}

if err := file.SetPluginDir(cfg.PluginDir); err != nil {
if err = SetPluginDir(cfg.PluginDir); err != nil {
log.Errorf("Error: %s.", err)
}

Expand Down
3 changes: 1 addition & 2 deletions internal/pkg/pluginengine/cmd_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/devstream-io/devstream/internal/pkg/configmanager"
"github.com/devstream-io/devstream/internal/pkg/statemanager"
"github.com/devstream-io/devstream/pkg/util/file"
"github.com/devstream-io/devstream/pkg/util/interact"
"github.com/devstream-io/devstream/pkg/util/log"
)
Expand All @@ -21,7 +20,7 @@ func Destroy(configFile string, continueDirectly bool, isForceDestroy bool) erro
return fmt.Errorf("failed to load the config file")
}

if err := file.SetPluginDir(cfg.PluginDir); err != nil {
if err = SetPluginDir(cfg.PluginDir); err != nil {
log.Errorf("Error: %s.", err)
}

Expand Down
3 changes: 1 addition & 2 deletions internal/pkg/pluginengine/cmd_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/devstream-io/devstream/internal/pkg/configmanager"
"github.com/devstream-io/devstream/internal/pkg/pluginmanager"
"github.com/devstream-io/devstream/internal/pkg/statemanager"
"github.com/devstream-io/devstream/pkg/util/file"
"github.com/devstream-io/devstream/pkg/util/log"
)

Expand All @@ -20,7 +19,7 @@ func Verify(configFile string) bool {
return false
}

if err := file.SetPluginDir(cfg.PluginDir); err != nil {
if err = SetPluginDir(cfg.PluginDir); err != nil {
log.Errorf("Error: %s.", err)
}

Expand Down
10 changes: 0 additions & 10 deletions internal/pkg/pluginengine/const.go

This file was deleted.

56 changes: 54 additions & 2 deletions internal/pkg/pluginengine/plugin.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package pluginengine

import (
"github.com/spf13/viper"
"os"
"path/filepath"
"strings"

"github.com/devstream-io/devstream/internal/pkg/statemanager"
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"

"github.com/devstream-io/devstream/internal/pkg/configmanager"
"github.com/devstream-io/devstream/internal/pkg/statemanager"
"github.com/devstream-io/devstream/pkg/util/log"
)

// DevStreamPlugin is a struct, on which Create/Read/Update/Delete interfaces are defined.
Expand Down Expand Up @@ -64,3 +69,50 @@ func Delete(tool *configmanager.Tool) (bool, error) {
func renderInstanceIDtoOptions(tool *configmanager.Tool) {
tool.Options["instanceID"] = interface{}(tool.InstanceID)
}

func SetPluginDir(conf string) error {
pluginDir, err := getPluginDir(conf)
if err != nil {
return err
}
viper.Set("plugin-dir", pluginDir)
return nil
}

func getPluginDir(conf string) (string, error) {
pluginDir := viper.GetString("plugin-dir")
if pluginDir == "" {
pluginDir = conf
}

if pluginDir == "" {
homeDir, err := homedir.Dir()
if err != nil {
return "", err
}
defaultPluginDir := filepath.Join(homeDir, ".devstream", "plugins")
return defaultPluginDir, nil
}

pluginRealDir, err := handlePathWithHome(pluginDir)
if err != nil {
return "", err
}
return pluginRealDir, nil
}

// handlePathWithHome deal with "~" in the filePath
func handlePathWithHome(filePath string) (string, error) {
if !strings.Contains(filePath, "~") {
return filePath, nil
}

homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
retPath := filepath.Join(homeDir, strings.TrimPrefix(filePath, "~"))
log.Debugf("real path: %s.", retPath)

return retPath, nil
}
3 changes: 1 addition & 2 deletions internal/pkg/show/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/devstream-io/devstream/internal/pkg/configmanager"
"github.com/devstream-io/devstream/internal/pkg/pluginengine"
"github.com/devstream-io/devstream/internal/pkg/statemanager"
"github.com/devstream-io/devstream/pkg/util/file"
"github.com/devstream-io/devstream/pkg/util/log"
)

Expand All @@ -36,7 +35,7 @@ func Show(configFile string) error {
return fmt.Errorf("failed to load the config file")
}

if err := file.SetPluginDir(cfg.PluginDir); err != nil {
if err = pluginengine.SetPluginDir(cfg.PluginDir); err != nil {
log.Errorf("Error: %s.", err)
}

Expand Down
57 changes: 0 additions & 57 deletions pkg/util/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,10 @@ package file
import (
"io"
"os"
"path/filepath"
"strings"

"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"

"github.com/devstream-io/devstream/pkg/util/log"
)

var DefaultPluginDir string

func init() {
homeDir, err := homedir.Dir()
if err != nil {
log.Fatalf("failed to get home dir: %s", err)
}
DefaultPluginDir = filepath.Join(homeDir, ".devstream", "plugins")
}

func GetPluginDir(conf string) (string, error) {
pluginDir := viper.GetString("plugin-dir")
if pluginDir == "" {
pluginDir = conf
}

if pluginDir == "" {
return DefaultPluginDir, nil
}

pluginRealDir, err := HandlePathWithHome(pluginDir)
if err != nil {
return "", err
}
return pluginRealDir, nil
}

// HandlePathWithHome deal with "~" in the filePath
func HandlePathWithHome(filePath string) (string, error) {
if !strings.Contains(filePath, "~") {
return filePath, nil
}

homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
retPath := filepath.Join(homeDir, strings.TrimPrefix(filePath, "~"))
log.Debugf("real path: %s.", retPath)

return retPath, nil
}

func SetPluginDir(conf string) error {
pluginDir, err := GetPluginDir(conf)
if err != nil {
return err
}
viper.Set("plugin-dir", pluginDir)
return nil
}

func CopyFile(srcFile, dstFile string) (err error) {
// prepare source file
sFile, err := os.Open(srcFile)
Expand Down