|
| 1 | +package jetbrains |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/jfrog/gofrog/log" |
| 8 | + "github.com/jfrog/jfrog-cli-artifactory/artifactory/cli/ide" |
| 9 | + "github.com/jfrog/jfrog-cli-artifactory/artifactory/commands/ide/jetbrains" |
| 10 | + pluginsCommon "github.com/jfrog/jfrog-cli-core/v2/plugins/common" |
| 11 | + "github.com/jfrog/jfrog-cli-core/v2/plugins/components" |
| 12 | + "github.com/jfrog/jfrog-cli-core/v2/utils/config" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + repoKeyFlag = "repo-key" |
| 17 | + urlSuffixFlag = "url-suffix" |
| 18 | + apiType = "jetbrainsplugins" |
| 19 | +) |
| 20 | + |
| 21 | +func GetCommands() []components.Command { |
| 22 | + return []components.Command{ |
| 23 | + { |
| 24 | + Name: "jetbrains-config", |
| 25 | + Aliases: []string{"jb"}, |
| 26 | + Hidden: true, |
| 27 | + Flags: getFlags(), |
| 28 | + Arguments: getArguments(), |
| 29 | + Action: jetbrainsConfigCmd, |
| 30 | + Description: ide.JetbrainsConfigDescription, |
| 31 | + }, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func getFlags() []components.Flag { |
| 36 | + return []components.Flag{ |
| 37 | + components.NewStringFlag(repoKeyFlag, "Repository key for the JetBrains plugins repo. [Required if no URL is given]", components.SetMandatoryFalse()), |
| 38 | + components.NewStringFlag(urlSuffixFlag, "Suffix for the JetBrains plugins repository URL. Default: (empty)", components.SetMandatoryFalse()), |
| 39 | + // Server configuration flags |
| 40 | + components.NewStringFlag("url", "JFrog Artifactory URL. (example: https://acme.jfrog.io/artifactory)", components.SetMandatoryFalse()), |
| 41 | + components.NewStringFlag("user", "JFrog username.", components.SetMandatoryFalse()), |
| 42 | + components.NewStringFlag("password", "JFrog password.", components.SetMandatoryFalse()), |
| 43 | + components.NewStringFlag("access-token", "JFrog access token.", components.SetMandatoryFalse()), |
| 44 | + components.NewStringFlag("server-id", "Server ID configured using the 'jf config' command.", components.SetMandatoryFalse()), |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func getArguments() []components.Argument { |
| 49 | + return []components.Argument{ |
| 50 | + { |
| 51 | + Name: "repository-url", |
| 52 | + Description: "The Artifactory JetBrains plugins repository URL (optional when using --repo-key)", |
| 53 | + Optional: true, |
| 54 | + }, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Main command action: orchestrates argument parsing, server config, and command execution |
| 59 | +func jetbrainsConfigCmd(c *components.Context) error { |
| 60 | + repoKey, repositoryURL, err := getJetbrainsRepoKeyAndURL(c) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + rtDetails, err := getJetbrainsServerDetails(c) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + jetbrainsCmd := jetbrains.NewJetbrainsCommand(repositoryURL, repoKey) |
| 71 | + |
| 72 | + // Determine if this is a direct URL (argument provided) vs constructed URL (server-id + repo-key) |
| 73 | + isDirectURL := c.GetNumberOfArgs() > 0 && ide.IsValidUrl(c.GetArgumentAt(0)) |
| 74 | + jetbrainsCmd.SetDirectURL(isDirectURL) |
| 75 | + |
| 76 | + if rtDetails != nil { |
| 77 | + jetbrainsCmd.SetServerDetails(rtDetails) |
| 78 | + } |
| 79 | + |
| 80 | + return jetbrainsCmd.Run() |
| 81 | +} |
| 82 | + |
| 83 | +// getJetbrainsRepoKeyAndURL determines the repo key and repository URL from args/flags |
| 84 | +func getJetbrainsRepoKeyAndURL(c *components.Context) (repoKey, repositoryURL string, err error) { |
| 85 | + if c.GetNumberOfArgs() > 0 && ide.IsValidUrl(c.GetArgumentAt(0)) { |
| 86 | + repositoryURL = c.GetArgumentAt(0) |
| 87 | + repoKey, err = ide.ExtractRepoKeyFromURL(repositoryURL) |
| 88 | + if err != nil { |
| 89 | + return |
| 90 | + } |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + repoKey = c.GetStringFlagValue(repoKeyFlag) |
| 95 | + if repoKey == "" { |
| 96 | + err = fmt.Errorf("You must provide either a repository URL as the first argument or --repo-key flag.") |
| 97 | + return |
| 98 | + } |
| 99 | + // Get Artifactory URL from server details (flags or default) |
| 100 | + var artDetails *config.ServerDetails |
| 101 | + if ide.HasServerConfigFlags(c) { |
| 102 | + artDetails, err = pluginsCommon.CreateArtifactoryDetailsByFlags(c) |
| 103 | + if err != nil { |
| 104 | + err = fmt.Errorf("Failed to get Artifactory server details: %w", err) |
| 105 | + return |
| 106 | + } |
| 107 | + } else { |
| 108 | + artDetails, err = config.GetDefaultServerConf() |
| 109 | + if err != nil { |
| 110 | + err = fmt.Errorf("Failed to get default Artifactory server details: %w", err) |
| 111 | + return |
| 112 | + } |
| 113 | + } |
| 114 | + // Use ArtifactoryUrl if available (when using flags), otherwise use Url (when using config) |
| 115 | + baseUrl := artDetails.ArtifactoryUrl |
| 116 | + if baseUrl == "" { |
| 117 | + baseUrl = artDetails.Url |
| 118 | + } |
| 119 | + baseUrl = strings.TrimRight(baseUrl, "/") |
| 120 | + |
| 121 | + urlSuffix := c.GetStringFlagValue(urlSuffixFlag) |
| 122 | + if urlSuffix != "" { |
| 123 | + urlSuffix = "/" + strings.TrimLeft(urlSuffix, "/") |
| 124 | + } |
| 125 | + repositoryURL = baseUrl + "/api/jetbrainsplugins/" + repoKey + urlSuffix |
| 126 | + return |
| 127 | +} |
| 128 | + |
| 129 | +// getJetbrainsServerDetails returns server details for validation, or nil if not available |
| 130 | +func getJetbrainsServerDetails(c *components.Context) (*config.ServerDetails, error) { |
| 131 | + if ide.HasServerConfigFlags(c) { |
| 132 | + // Use explicit server configuration flags |
| 133 | + rtDetails, err := pluginsCommon.CreateArtifactoryDetailsByFlags(c) |
| 134 | + if err != nil { |
| 135 | + return nil, fmt.Errorf("failed to create server configuration: %w", err) |
| 136 | + } |
| 137 | + return rtDetails, nil |
| 138 | + } |
| 139 | + // Use default server configuration for validation when no explicit flags provided |
| 140 | + rtDetails, err := config.GetDefaultServerConf() |
| 141 | + if err != nil { |
| 142 | + // If no default server, that's okay - we'll just skip validation |
| 143 | + log.Debug("No default server configuration found, skipping repository validation") |
| 144 | + return nil, nil //nolint:nilerr // Intentionally ignoring error to skip validation when no default server |
| 145 | + } |
| 146 | + return rtDetails, nil |
| 147 | +} |
0 commit comments