-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathessentials.go
More file actions
76 lines (59 loc) · 2.87 KB
/
Copy pathessentials.go
File metadata and controls
76 lines (59 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2023 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package pluginmanager
import (
"fmt"
"os"
"github.com/vmware-tanzu/tanzu-cli/pkg/constants"
"github.com/vmware-tanzu/tanzu-cli/pkg/essentials"
"github.com/vmware-tanzu/tanzu-plugin-runtime/log"
)
// InstallPluginsFromEssentialPluginGroup is a function that installs or upgrades the essential plugin groups.
func InstallPluginsFromEssentialPluginGroup() (string, error) {
// Retrieve the name and version of the essential plugin group.
name, version := essentials.GetEssentialsPluginGroupDetails()
// Check if the plugins from the plugin group are installed, and if an update is available.
installed, updateAvailable, err := IsPluginsFromPluginGroupInstalled(name, version, DisableLogs())
// If there's an error, return it with additional context.
if err != nil {
return "", fmt.Errorf("failed to check if plugins from group are installed: %w", err)
}
// If the plugins are already installed and no update is available, return with no error.
if installed && !updateAvailable {
return "", nil
}
// If an update is available, log a message indicating that the essential plugin groups are being upgraded.
// If the plugins are not installed, log a message indicating that the essential plugin groups are being installed.
actionMessage := constants.InstallEssentialPluginGroupsMsg
if updateAvailable {
actionMessage = constants.UpgradeEssentialPluginGroupsMsg
}
log.Info(actionMessage)
// Attempt to install or upgrade the essential plugin group.
_, err = installPluginsFromEssentialPluginGroup(name, version)
// Print an empty line to delimit the essential plugins output with any actual command output
fmt.Fprintln(os.Stderr)
// If there's an error during installation or upgrade, return it with additional context.
if err != nil {
return "", fmt.Errorf("failed to install or upgrade essential plugin group: %w", err)
}
// If the installation or upgrade is successful, return with no error.
return "", nil
}
// installEssentialPluginGroup is a function that installs the essential plugin group.
func installPluginsFromEssentialPluginGroup(name, version string) (string, error) {
pluginGroupNameWithVersion := name
// Combine the name and version into a single string.
if version != "" {
pluginGroupNameWithVersion = fmt.Sprintf("%v:%v", pluginGroupNameWithVersion, version)
}
// Attempt to install the plugins from the group.
groupWithVersion, err := InstallPluginsFromGroup("all", pluginGroupNameWithVersion, DisableLogs())
// If there's an error during installation, return it with additional context.
if err != nil {
return "", fmt.Errorf("failed to install plugins from group: %w", err)
}
log.Successf("successfully installed all plugins from group '%s'", groupWithVersion)
// If the installation is successful, return the group with version.
return groupWithVersion, nil
}