Skip to content

Commit

Permalink
Add API to manage plugin owned settings (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpanchajanya authored Sep 27, 2023
1 parent 39c403b commit 923c4c1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ of the main packages that each and every plugin will need to import to
integrate with the Tanzu CLI. For more information about
the development process, see the [Tanzu CLI Plugin Development guide](https://github.com/vmware-tanzu/tanzu-cli/blob/main/docs/plugindev/README.md)

To Manage plugin owned settings

Use

``` go
// Plugin owned APIs
// GetTanzuPluginConfigDir Retrieve the tanzu configuration directory that can be used by the plugins to
// create a plugin specific directory to manage plugin owned configurations.
func GetTanzuPluginConfigDir() (string, error)
```

Example: For handling settings specific to the `management-cluster` plugin:
Utilize `GetTanzuPluginConfigDir()` to retrieve the plugin configuration directory,
which is `.config/tanzu/plugins`. Subsequently, establish a `management-cluster` directory within the plugin configuration directory to oversee the relevant settings.

### Command Helpers

This package implements command specific helper functions like command deprecation, etc.
Expand Down
34 changes: 34 additions & 0 deletions config/plugin-owned.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2023 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package config

import (
"os"
"path/filepath"

"github.com/pkg/errors"
)

var (
// PluginsBaseDir is the name of the plugins owned base directory in which plugin owned settings is stored.
PluginsBaseDir = "plugins"
)

// GetTanzuPluginConfigDir Retrieve the tanzu configuration directory that can be used by the plugins to // create a plugin specific directory to manage plugin owned configurations.
// .config/tanzu/plugins
func GetTanzuPluginConfigDir() (string, error) {
// Fetch the base tanzu config directory
tanzuDir, err := LocalDir()
if err != nil {
return "", errors.Wrap(err, "could not find local tanzu dir for OS")
}

// Create plugins directory in tanzu config
pluginsBaseDir := filepath.Join(tanzuDir, PluginsBaseDir)
if err := os.MkdirAll(pluginsBaseDir, 0755); err != nil {
return "", errors.Wrap(err, "could not make local tanzu plugins directory")
}

return pluginsBaseDir, nil
}
30 changes: 30 additions & 0 deletions config/plugin-owned_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2023 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package config

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGetTanzuPluginConfigDir(t *testing.T) {
// setup
func() {
LocalDirName = ".config2/tanzu"
}()
defer func() {
cleanupDir(LocalDirName)
}()

expectedPath := ".config2/tanzu/plugins"

dir, err := GetTanzuPluginConfigDir()

require.NoError(t, err, "Expected no error from GetTanzuPluginConfigDir")
assert.NotEmpty(t, dir, "Expected a non-empty directory path")

assert.Contains(t, dir, expectedPath)
}
5 changes: 5 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ said information in the files being manipulated.
### Available Runtime Config APIs

``` go
// Plugin owned APIs
// GetTanzuPluginConfigDir Retrieve the tanzu configuration directory that can be used by the plugins to
// create a plugin specific directory to manage plugin owned configurations.
func GetTanzuPluginConfigDir() (string, error)

// Context APIs
func GetContext(name string) (context Context, error)
func AddContext(context Context, setCurrent bool) error
Expand Down

0 comments on commit 923c4c1

Please sign in to comment.