|
| 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 languagecontainer defines LanguageContainer interface and |
| 16 | +// the Run function to execute commands within the container. |
| 17 | +// TODO(b/447404382): Move this package to the https://github.com/googleapis/librarian |
| 18 | +// GitHub repository once the interface is finalized. |
| 19 | +// This package must not have any Java-specific implementation. |
| 20 | +package languagecontainer |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "log/slog" |
| 25 | + |
| 26 | + "cloud.google.com/java/internal/librariangen/message" |
| 27 | +) |
| 28 | + |
| 29 | +// GenerateCommandContext holds the context (the file system paths) for |
| 30 | +// the generate command. The createGenerateContext function creates |
| 31 | +// an instance of this by reading the command line flags and the |
| 32 | +// default values. |
| 33 | +type GenerateCommandContext struct { |
| 34 | +} |
| 35 | +type GenerateCommandEnv struct { |
| 36 | + GenerateContext *GenerateCommandContext |
| 37 | + GenerateRequest *message.GenerateRequest |
| 38 | +} |
| 39 | + |
| 40 | +type ConfigureCommandContext struct { |
| 41 | +} |
| 42 | + |
| 43 | +type ConfigureCommandEnv struct { |
| 44 | + ConfigureContext *ConfigureCommandContext |
| 45 | + ConfigureRequest *message.ConfigureRequest |
| 46 | +} |
| 47 | + |
| 48 | +// LanguageContainer defines the interface for language-specific container operations. |
| 49 | +type LanguageContainer interface { |
| 50 | + Generate(context.Context, *GenerateCommandEnv) error |
| 51 | + Configure(context.Context, *ConfigureCommandEnv) (*message.ConfigureResponse, error) |
| 52 | + // Other container functions like ReleaseInit and Build would also be part of the interface. |
| 53 | +} |
| 54 | + |
| 55 | +// Run would accept an implementation of the LanguageContainer interface. |
| 56 | +func Run(args []string, container LanguageContainer) int { |
| 57 | + // Logic to parse args and call the appropriate method on the container. |
| 58 | + // For example, if args[1] is "generate": |
| 59 | + // request := ... // unmarshal the request from the expected location |
| 60 | + // err := container.Generate(context.Background(), request) |
| 61 | + // ... |
| 62 | + if len(args) < 1 { |
| 63 | + panic("args must not be empty") |
| 64 | + } |
| 65 | + switch args[0] { |
| 66 | + case "generate": |
| 67 | + env := createGenerateEnv(args[1:]) |
| 68 | + err := container.Generate(context.Background(), env) |
| 69 | + if err != nil { |
| 70 | + // TODO: Save it as a response file. |
| 71 | + panic(err) |
| 72 | + } |
| 73 | + return 0 |
| 74 | + case "configure": |
| 75 | + env := createConfigureEnv(args[1:]) |
| 76 | + resp, err := container.Configure(context.Background(), env) |
| 77 | + if err != nil { |
| 78 | + panic(err) |
| 79 | + } |
| 80 | + // TODO: Save it as a response file. |
| 81 | + _ = resp |
| 82 | + return 0 |
| 83 | + case "release-init": |
| 84 | + slog.Warn("librariangen: release-init command is not yet implemented") |
| 85 | + return 1 |
| 86 | + case "build": |
| 87 | + slog.Warn("librariangen: build command is not yet implemented") |
| 88 | + return 1 |
| 89 | + default: |
| 90 | + slog.Error("librariangen: unknown command: %s (with flags %v)", args[0], args) |
| 91 | + return 1 |
| 92 | + } |
| 93 | + return 0 |
| 94 | +} |
| 95 | + |
| 96 | +// https://github.com/googleapis/librarian/blob/main/doc/language-onboarding.md#generate |
| 97 | +func createGenerateEnv(args []string) *GenerateCommandEnv { |
| 98 | + generateContext := createGenerateCommandContext(args) |
| 99 | + return &GenerateCommandEnv{ |
| 100 | + GenerateContext: generateContext, |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func createGenerateCommandContext(args []string) *GenerateCommandContext { |
| 105 | + // TODO: Parse args and create the context. |
| 106 | + return &GenerateCommandContext{} |
| 107 | +} |
| 108 | + |
| 109 | +func createConfigureCommandContext(args []string) *ConfigureCommandContext { |
| 110 | + // TODO: Parse args and create the context. |
| 111 | + return &ConfigureCommandContext{} |
| 112 | +} |
| 113 | + |
| 114 | +func createConfigureEnv(args []string) *ConfigureCommandEnv { |
| 115 | + configureContext := createConfigureCommandContext(args) |
| 116 | + return &ConfigureCommandEnv{ |
| 117 | + ConfigureContext: configureContext, |
| 118 | + } |
| 119 | +} |
0 commit comments