Skip to content
Open
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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ gcloud components install gke-gcloud-auth-plugin
gcloud components install cloud-sql-proxy
```

### Default Cloud SDK Components

You can define a set of Cloud SDK components to be installed automatically every
time a new gcloud version is installed. Create a file named
`.default-cloud-sdk-components` with one component ID per line.

The plugin searches for this file in the following locations (in order):

1. `$CLOUDSDK_CONFIG/.default-cloud-sdk-components` (defaults to `~/.config/gcloud/` on Unix or `%APPDATA%\gcloud\` on Windows)
2. `$HOME/.default-cloud-sdk-components`

Example `~/.default-cloud-sdk-components`:

```
alpha
beta
cloud-firestore-emulator
gke-gcloud-auth-plugin
```

## Supported Platforms

- Linux (x86_64, ARM)
Expand Down
72 changes: 72 additions & 0 deletions hooks/post_install.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
--- @field ctx.rootPath string The installation directory

local file = require("file")
local os = require("os")
local log = require("log")
local strings = require("strings")

--- Compare version strings
--- Returns true if v1 >= v2
Expand Down Expand Up @@ -30,6 +33,68 @@ local function version_gte(v1, v2)
return true
end

local function get_home_dir()
return os.getenv("HOME") or os.getenv("USERPROFILE") or ""
end

local function find_default_components_file()
local filename = ".default-cloud-sdk-components"
local home = get_home_dir()
local cloudsdk_config = os.getenv("CLOUDSDK_CONFIG")
if not cloudsdk_config or cloudsdk_config == "" then
if RUNTIME.osType == "windows" or RUNTIME.osType == "Windows" then
cloudsdk_config = file.join_path(os.getenv("APPDATA") or home, "gcloud")
else
cloudsdk_config = file.join_path(home, ".config", "gcloud")
end
end
Comment thread
flou marked this conversation as resolved.

for _, dir in ipairs({ cloudsdk_config, home }) do
local path = file.join_path(dir, filename)
if file.exists(path) then
return path
end
end
end

local function install_default_components(gcloud_bin)
local components_file = find_default_components_file()
if not components_file then
return
end

log.info("Installing default Cloud SDK components from " .. components_file)

local contents = file.read(components_file)
if not contents or contents == "" then
return
end

local components = {}
for _, line in ipairs(strings.split(contents, "\n")) do
local trimmed = strings.trim_space(line)
if trimmed ~= "" and not string.find(trimmed, "^#") then
if string.match(trimmed, "^[%w%-_%.]+$") then
table.insert(components, trimmed)
else
log.info("Skipping invalid component name: " .. trimmed)
end
end
Comment thread
flou marked this conversation as resolved.
end

if #components == 0 then
return
end

local cmd = string.format('"%s" --quiet components install %s', gcloud_bin, table.concat(components, " "))
local status = os.execute(cmd)
if status ~= 0 and status ~= true then
log.error("Failed to install default Cloud SDK components")
return
end
log.info("Default Cloud SDK components installed successfully")
end

function PLUGIN:PostInstall(ctx)
local sdkInfo = ctx.sdkInfo[PLUGIN.name]
local root_path = sdkInfo.path
Expand Down Expand Up @@ -80,4 +145,11 @@ function PLUGIN:PostInstall(ctx)
if status ~= 0 and status ~= true then
error("Failed to run gcloud install script")
end

-- Install default SDK components
local gcloud_bin = file.join_path(sdk_path, "bin", "gcloud")
Comment thread
flou marked this conversation as resolved.
if RUNTIME.osType == "windows" or RUNTIME.osType == "Windows" then
gcloud_bin = gcloud_bin .. ".cmd"
end
install_default_components(gcloud_bin)
end