Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bootstrap repo, NexusService.StartOperation #1

Closed
wants to merge 2 commits into from
Closed
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
46 changes: 46 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Continuous Integration

on: # rebuild any PRs and main branch changes
pull_request:
push:
branches:
- main
- "releases/*"

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
lint-and-test:
strategy:
fail-fast: false
matrix:
# Verify scripts run on any OS
os: [ubuntu-latest, macos-latest, windows-latest]

runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
# TODO: GH cache got messed up, need to figure out how to bump this version
# with:
# go-version: 1.20
- name: Install protoc
uses: arduino/setup-protoc@v2
with:
version: "23.x"
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Build CLI
run: go build -o ${{ startsWith(matrix.os, 'windows-') && 'scripts.exe' || './scripts' }} ./cmd
- name: Download tool dependencies
run: ${{ env.SCRIPTS }} install-deps
env:
SCRIPTS: ${{ startsWith(matrix.os, 'windows-') && 'start scripts.exe' || './scripts' }}
- name: Lint
run: ${{ env.SCRIPTS }} lint
env:
SCRIPTS: ${{ startsWith(matrix.os, 'windows-') && 'start scripts.exe' || './scripts' }}
- name: Test
run: ${{ env.SCRIPTS }} test
env:
SCRIPTS: ${{ startsWith(matrix.os, 'windows-') && 'start scripts.exe' || './scripts' }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scripts
23 changes: 23 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Nexus API

MIT License

Copyright (c) 2023 Temporal Technologies Inc. All Rights Reserved

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Nexus API

Protocol Buffers API definitions for Nexus RPCs.

### Prerequisites

- [go](https://go.dev/doc/install)
- [protoc](https://grpc.io/docs/protoc-installation/)

### Install lint and test tool dependencies

```shell
go run ./cmd install-deps
```

### Lint

Lint with:

- [buf](https://buf.build/docs/installation)
- [api-linter](https://github.com/googleapis/api-linter)

```shell
go run ./cmd lint
```

### Test compilation

Test protoc compilation for select languages.

```shell
go run ./cmd test
```
17 changes: 17 additions & 0 deletions api-linter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
- included_paths:
- "**/*.proto"
disabled_rules:
- "core::0127::resource-name-extraction"
- "core::0131::method-signature"
- "core::0131::http-uri-name"
- "core::0131::request-name-required"
- "core::0131::request-required-fields"
- "core::0131::request-unknown-fields"
- "core::0136::http-body"
- "core::0136::http-uri-suffix"
- "core::0192::has-comments"

- included_paths:
- "dependencies/gogoproto/gogo.proto"
disabled_rules:
- "all"
11 changes: 11 additions & 0 deletions buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: v1
breaking:
use:
- WIRE_JSON
lint:
use:
- DEFAULT
except:
- RPC_RESPONSE_STANDARD_NAME
ignore:
- google
58 changes: 58 additions & 0 deletions cmd/install_deps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"context"
"os"
"os/exec"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.uber.org/zap"
)

var deps = []string{
"github.com/bufbuild/buf/cmd/[email protected]",
"github.com/googleapis/api-linter/cmd/[email protected]",
"google.golang.org/protobuf/cmd/[email protected]",
}

func installDepsCmd() *cobra.Command {
var i depsInstaller
cmd := &cobra.Command{
Use: "install-deps",
Short: "Install tool dependencies",
Run: func(cmd *cobra.Command, args []string) {
if err := i.installDeps(cmd.Context()); err != nil {
i.logger.Fatal(err)
}
},
}
i.addCLIFlags(cmd.Flags())
return cmd
}

type depsInstaller struct {
logger *zap.SugaredLogger
loggingOptions LoggingOptions
}

func (l *depsInstaller) addCLIFlags(fs *pflag.FlagSet) {
l.loggingOptions.AddCLIFlags(fs)
}

func (l *depsInstaller) installDeps(ctx context.Context) error {
l.logger = l.loggingOptions.MustCreateLogger()

for _, dep := range deps {
args := []string{"install", dep}

l.logger.Infow("Installing dependency", "dep", dep)
cmd := exec.CommandContext(ctx, "go", args...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}
56 changes: 56 additions & 0 deletions cmd/lint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"context"
"os"
"os/exec"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.uber.org/zap"
)

func lintCmd() *cobra.Command {
var l linter
cmd := &cobra.Command{
Use: "lint",
Short: "Lint proto files",
Run: func(cmd *cobra.Command, args []string) {
if err := l.lint(cmd.Context()); err != nil {
l.logger.Fatal(err)
}
},
}
l.addCLIFlags(cmd.Flags())
return cmd
}

type linter struct {
logger *zap.SugaredLogger
loggingOptions LoggingOptions
}

func (l *linter) addCLIFlags(fs *pflag.FlagSet) {
l.loggingOptions.AddCLIFlags(fs)
}

func (l *linter) lint(ctx context.Context) error {
l.logger = l.loggingOptions.MustCreateLogger()
protoFiles, err := findProtos()
if err != nil {
return err
}
args := append([]string{"--set-exit-status", "--config", "./api-linter.yaml", "-I", "."}, protoFiles...)
l.logger.Infow("Running api-linter", "args", args)
cmd := exec.CommandContext(ctx, "api-linter", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}
l.logger.Infow("Running buf lint")
cmd = exec.CommandContext(ctx, "buf", "lint")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
47 changes: 47 additions & 0 deletions cmd/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"log"
"os"

"github.com/spf13/pflag"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// LoggingOptions for setting up the logger component
type LoggingOptions struct {
// Log level
LogLevel string
// Log encoding (console json)
LogEncoding string
}

// BackupLogger is used in case we can't instantiate zap (it's nicer DX than panicking or using built-in `log`).
var BackupLogger = log.New(os.Stderr, "", 0)

// MustCreateLogger sets up a zap logger or panics on error.
func (l *LoggingOptions) MustCreateLogger() *zap.SugaredLogger {
level, err := zap.ParseAtomicLevel(l.LogLevel)
if err != nil {
BackupLogger.Fatalf("Invalid log level: %v", err)
}
logger, err := zap.Config{
Level: level,
Encoding: l.LogEncoding,
EncoderConfig: zap.NewDevelopmentEncoderConfig(),
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}.Build(zap.AddStacktrace(zapcore.FatalLevel))
if err != nil {
BackupLogger.Fatalf("Failed to initialize logger: %v", err)
}

return logger.Sugar()
}

// AddCLIFlags adds the relevant flags to populate the options struct.
func (l *LoggingOptions) AddCLIFlags(fs *pflag.FlagSet) {
fs.StringVar(&l.LogLevel, "log-level", "info", "(debug info warn error panic fatal)")
fs.StringVar(&l.LogEncoding, "log-encoding", "console", "(console json)")
}
20 changes: 20 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"github.com/spf13/cobra"
)

func main() {
var rootCmd = &cobra.Command{
Use: "api",
Short: "Scripts for the Nexus API repo",
}

rootCmd.AddCommand(lintCmd())
rootCmd.AddCommand(testCmd())
rootCmd.AddCommand(installDepsCmd())

if err := rootCmd.Execute(); err != nil {
BackupLogger.Fatalf("Failed to run %s", err)
}
}
25 changes: 25 additions & 0 deletions cmd/protos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"os"
"path/filepath"
"runtime"
)

func projectRoot() string {
_, filename, _, _ := runtime.Caller(0)
return filepath.Dir(filepath.Dir(filename))
}

func findProtos() ([]string, error) {
dir := filepath.Join(projectRoot(), "nexus")
files := []string{}
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if filepath.Ext(path) == ".proto" {
files = append(files, path)
}
return nil
})

return files, err
}
Loading