mirror of
https://github.com/onyx-and-iris/ignr.git
synced 2026-04-18 07:13:33 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bae71b983e | |||
| f4ddb45fd2 | |||
| cad1585975 | |||
| 4139c4b291 |
@@ -10,13 +10,13 @@ type contextKey string
|
|||||||
|
|
||||||
var clientKey = contextKey("client")
|
var clientKey = contextKey("client")
|
||||||
|
|
||||||
// withClient returns a new context with the GitHub client set.
|
// WithClient returns a new context with the GitHub client set.
|
||||||
func withClient(ctx context.Context, client *github.Client) context.Context {
|
func WithClient(ctx context.Context, client *github.Client) context.Context {
|
||||||
return context.WithValue(ctx, clientKey, client)
|
return context.WithValue(ctx, clientKey, client)
|
||||||
}
|
}
|
||||||
|
|
||||||
// clientFromContext retrieves the GitHub client from the context.
|
// ClientFromContext retrieves the GitHub client from the context.
|
||||||
func clientFromContext(ctx context.Context) (*github.Client, bool) {
|
func ClientFromContext(ctx context.Context) (*github.Client, bool) {
|
||||||
client, ok := ctx.Value(clientKey).(*github.Client)
|
client, ok := ctx.Value(clientKey).(*github.Client)
|
||||||
return client, ok
|
return client, ok
|
||||||
}
|
}
|
||||||
|
|||||||
20
main.go
20
main.go
@@ -4,6 +4,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/charmbracelet/fang"
|
"github.com/charmbracelet/fang"
|
||||||
@@ -29,11 +30,8 @@ You may also list available templates and generate .gitignore files based on tho
|
|||||||
} else {
|
} else {
|
||||||
client = github.NewClient(nil).WithAuthToken(viper.GetString("token"))
|
client = github.NewClient(nil).WithAuthToken(viper.GetString("token"))
|
||||||
}
|
}
|
||||||
ctx := withClient(cmd.Context(), client)
|
cmd.SetContext(WithClient(cmd.Context(), client))
|
||||||
cmd.SetContext(ctx)
|
|
||||||
},
|
},
|
||||||
//RunE: func(cmd *cobra.Command, _ []string) error {
|
|
||||||
//},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// init initialises the root command and its flags.
|
// init initialises the root command and its flags.
|
||||||
@@ -56,7 +54,19 @@ func init() {
|
|||||||
// main is the entry point of the application.
|
// main is the entry point of the application.
|
||||||
// It executes the root command and handles any errors.
|
// It executes the root command and handles any errors.
|
||||||
func main() {
|
func main() {
|
||||||
if err := fang.Execute(context.Background(), rootCmd, fang.WithVersion(version)); err != nil {
|
if err := fang.Execute(context.Background(), rootCmd, fang.WithVersion(versionFromBuild())); err != nil {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func versionFromBuild() string {
|
||||||
|
if version == "" {
|
||||||
|
info, ok := debug.ReadBuildInfo()
|
||||||
|
if !ok {
|
||||||
|
return "(unable to read version)"
|
||||||
|
}
|
||||||
|
version = strings.Split(info.Main.Version, "-")[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
return version
|
||||||
|
}
|
||||||
|
|||||||
4
new.go
4
new.go
@@ -13,7 +13,7 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
const gitignoreFileName = ".gitignore"
|
const gitignoreFileName string = ".gitignore"
|
||||||
|
|
||||||
// newCmd represents the new command.
|
// newCmd represents the new command.
|
||||||
var newCmd = &cobra.Command{
|
var newCmd = &cobra.Command{
|
||||||
@@ -47,7 +47,7 @@ func runNewCommand(cmd *cobra.Command, _ []string) error {
|
|||||||
return errors.New("height must be a positive integer")
|
return errors.New("height must be a positive integer")
|
||||||
}
|
}
|
||||||
|
|
||||||
client, ok := clientFromContext(cmd.Context())
|
client, ok := ClientFromContext(cmd.Context())
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("failed to get GitHub client from context")
|
return errors.New("failed to get GitHub client from context")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user