Skip to content

Commit

Permalink
Adding first example
Browse files Browse the repository at this point in the history
This PR includes the source code for the tutorial that is hosted
here: https://bazel-contrib.github.io/SIG-rules-authors/go-tutorial.html

The code for the above html is on the gh-pages branch of the
SIG-rules-authors repository:
https://github.com/bazel-contrib/SIG-rules-authors/tree/gh-pages

This tutorial source code includes:

- basic go rules support
- a basic cobra CLI application
- unit test
- Gazelle support

After this PR is finalized we I will update the tutorial markdown.
  • Loading branch information
chrislovecnm committed Oct 10, 2022
1 parent d517cd9 commit ac5d64c
Show file tree
Hide file tree
Showing 17 changed files with 641 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/go-code-tutorial/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/bazel-bazel-gazelle
/bazel-bin
/bazel-out
/bazel-go-code-tutorial
/bazel-testlogs
30 changes: 30 additions & 0 deletions examples/go-code-tutorial/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@bazel_gazelle//:def.bzl", "gazelle")

# gazelle:prefix github.com/bazelbuild/rules_go/examples/go-code-tutorial
gazelle(name = "gazelle")

# adding rule to update deps
gazelle(
name = "gazelle-update-repos",
args = [
"-from_file=go.mod",
"-to_macro=deps.bzl%go_dependencies",
"-prune",
],
command = "update-repos",
)

go_library(
name = "go-code-tutorial_lib",
srcs = ["main.go"],
importpath = "github.com/bazelbuild/rules_go/examples/go-code-tutorial",
visibility = ["//visibility:private"],
deps = ["//cmd"],
)

go_binary(
name = "go-code-tutorial",
embed = [":go-code-tutorial_lib"],
visibility = ["//visibility:public"],
)
12 changes: 12 additions & 0 deletions examples/go-code-tutorial/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Go Rules Tutorial

This is the source code for the Go Rules tutorial that is available here:
https://bazel-contrib.github.io/SIG-rules-authors/go-tutorial.html

## Updates

Please update both the source code here and the tutorial that is on the gh-pages branch here:
https://github.com/bazel-contrib/SIG-rules-authors/tree/gh-pages.

Updates to this tutorial require two PRs, as there are code block in the gh-pages tutorial mentioned
above.
43 changes: 43 additions & 0 deletions examples/go-code-tutorial/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# download bazel rules_go

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "io_bazel_rules_go",
sha256 = "099a9fb96a376ccbbb7d291ed4ecbdfd42f6bc822ab77ae6f1b5cb9e914e94fa",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip",
],
)

# download bazel_gazel dependency
http_archive(
name = "bazel_gazelle",
sha256 = "efbbba6ac1a4fd342d5122cbdfdb82aeb2cf2862e35022c752eaddffada7c3f3",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.27.0/bazel-gazelle-v0.27.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.27.0/bazel-gazelle-v0.27.0.tar.gz",
],
)

# load bazel and gazelle rules
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")

############################################################
# Define your own dependencies here using go_repository.
# Else, dependencies declared by rules_go/gazelle will be used.
# The first declaration of an external repository "wins".
############################################################

load("//:deps.bzl", "go_dependencies")

# gazelle:repository_macro deps.bzl%go_dependencies
go_dependencies()

go_rules_dependencies()

go_register_toolchains(version = "1.19.1")

gazelle_dependencies()
17 changes: 17 additions & 0 deletions examples/go-code-tutorial/cmd/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "cmd",
srcs = [
"roll.go",
"root.go",
"word.go",
],
importpath = "github.com/bazelbuild/rules_go/examples/go-code-tutorial/cmd",
visibility = ["//visibility:public"],
deps = [
"//pkg/roll",
"//pkg/word",
"@com_github_spf13_cobra//:cobra",
],
)
41 changes: 41 additions & 0 deletions examples/go-code-tutorial/cmd/roll.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"

"github.com/bazelbuild/rules_go/examples/go-code-tutorial/pkg/roll"
"github.com/spf13/cobra"
)

// rollCmd represents the roll command
var rollCmd = &cobra.Command{
Use: "roll",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("roll called")
roll.Roll()
},
}

func init() {
rootCmd.AddCommand(rollCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// rollCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// rollCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
58 changes: 58 additions & 0 deletions examples/go-code-tutorial/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright © 2022 Chris Love [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"os"

"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "go-code-tutorial",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.go-example-code.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
41 changes: 41 additions & 0 deletions examples/go-code-tutorial/cmd/word.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"

"github.com/bazelbuild/rules_go/examples/go-code-tutorial/pkg/word"
"github.com/spf13/cobra"
)

// wordCmd represents the word command
var wordCmd = &cobra.Command{
Use: "word",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("word called")
fmt.Println(word.GenerateWord())
},
}

func init() {
rootCmd.AddCommand(wordCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// wordCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// wordCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
Loading

0 comments on commit ac5d64c

Please sign in to comment.