Skip to content

Commit f1bef15

Browse files
CLI for command build webui (#116)
* CLI for command build webui * Improved error handling & comments * Function name update * Naming and text fixes * Moved cli util to internal * Import fix for cli/util
1 parent 7d877d1 commit f1bef15

File tree

6 files changed

+142
-2
lines changed

6 files changed

+142
-2
lines changed

cmd/adkgo/adkgo.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ package main
1717

1818
import (
1919
"google.golang.org/adk/cmd/adkgo/root"
20+
_ "google.golang.org/adk/cmd/adkgo/root/build"
21+
_ "google.golang.org/adk/cmd/adkgo/root/build/webui"
2022
_ "google.golang.org/adk/cmd/adkgo/root/deploy"
2123
_ "google.golang.org/adk/cmd/adkgo/root/deploy/cloudrun"
24+
_ "google.golang.org/adk/cmd/adkgo/root/run"
2225
_ "google.golang.org/adk/cmd/adkgo/root/run/local"
2326
)
2427

cmd/adkgo/root/build/build.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// package build handles command line parameters for command build
16+
package build
17+
18+
import (
19+
"github.com/spf13/cobra"
20+
"google.golang.org/adk/cmd/adkgo/root"
21+
)
22+
23+
// deployCmd represents the deploy command
24+
var BuildCmd = &cobra.Command{
25+
Use: "build",
26+
Short: "Makes build easy",
27+
Long: `Please see subcommands for details`,
28+
Run: func(cmd *cobra.Command, args []string) {
29+
},
30+
}
31+
32+
func init() {
33+
root.RootCmd.AddCommand(BuildCmd)
34+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// package webui handles command line parameters and execution logic for build webui
16+
package webui
17+
18+
import (
19+
"fmt"
20+
"os"
21+
"os/exec"
22+
23+
"github.com/spf13/cobra"
24+
"google.golang.org/adk/cmd/adkgo/root/build"
25+
"google.golang.org/adk/internal/cli/util"
26+
)
27+
28+
type buildFlags struct {
29+
targetDir string // command line param
30+
}
31+
32+
type sourceFlags struct {
33+
webuiDir string // command line param
34+
}
35+
36+
type runLocalFlags struct {
37+
build buildFlags
38+
source sourceFlags
39+
}
40+
41+
var flags runLocalFlags
42+
43+
// webuiCmd represents the build webui command
44+
var webuiCmd = &cobra.Command{
45+
Use: "webui",
46+
Short: "Build static ADK Web UI from sources.",
47+
Long: `
48+
Builds static ADK Web UI files from sources.
49+
WARNINIG: deletes the whole build directory and recreates it anew!
50+
You need:
51+
- a downloaded version of adk-web (available at https://github.com/google/adk-web)
52+
- an ability to build adk-web (prerequisites on https://github.com/google/adk-web):
53+
npm (node js: see https://nodejs.org/en/download)
54+
ng (angular cli: see https://angular.dev/tools/cli/setup-local)
55+
- go
56+
57+
Building the adk-web takes a while, and sometimes presents some warnings.
58+
`,
59+
RunE: func(cmd *cobra.Command, args []string) error {
60+
err := flags.buildWebui()
61+
return err
62+
},
63+
}
64+
65+
func init() {
66+
build.BuildCmd.AddCommand(webuiCmd)
67+
68+
webuiCmd.PersistentFlags().StringVarP(&flags.build.targetDir, "targetDir", "t", "", "Target directory for build output")
69+
webuiCmd.PersistentFlags().StringVarP(&flags.source.webuiDir, "sourceDir", "s", "", "Directory containing ADK Web UI (from https://github.com/google/adk-web)")
70+
}
71+
72+
func (f *runLocalFlags) cleanTemp() error {
73+
return util.LogStartStop("Cleaning target directory",
74+
func(p util.Printer) error {
75+
p("Clean target directory starting with", f.build.targetDir)
76+
err := os.RemoveAll(f.build.targetDir)
77+
if err != nil {
78+
return fmt.Errorf("failed to clean target directory %v: %v", f.build.targetDir, err)
79+
}
80+
err = os.MkdirAll(f.build.targetDir, os.ModeDir|0700)
81+
if err != nil {
82+
return fmt.Errorf("failed to create the target directory %v: %v", f.build.targetDir, err)
83+
}
84+
return nil
85+
})
86+
}
87+
88+
func (f *runLocalFlags) ngBuildADKWebUI() error {
89+
return util.LogStartStop("Building ADK Web UI",
90+
func(p util.Printer) error {
91+
cmd := exec.Command("ng", "build", "--output-path="+f.build.targetDir)
92+
cmd.Dir = f.source.webuiDir
93+
return util.LogCommand(cmd, p)
94+
})
95+
}
96+
97+
func (f *runLocalFlags) buildWebui() error {
98+
err := f.cleanTemp()
99+
if err != nil {
100+
return err
101+
}
102+
return f.ngBuildADKWebUI()
103+
}

cmd/adkgo/root/deploy/cloudrun/cloudrun.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424

2525
"github.com/spf13/cobra"
2626
"google.golang.org/adk/cmd/adkgo/root/deploy"
27-
"google.golang.org/adk/cmd/util"
27+
"google.golang.org/adk/internal/cli/util"
2828
)
2929

3030
type gCloudFlags struct {

cmd/adkgo/root/run/local/local.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424

2525
"github.com/spf13/cobra"
2626
"google.golang.org/adk/cmd/adkgo/root/run"
27-
"google.golang.org/adk/cmd/util"
27+
"google.golang.org/adk/internal/cli/util"
2828
)
2929

3030
type localServerFlags struct {

0 commit comments

Comments
 (0)