feat: add cli flags
This commit is contained in:
18
cli/list.go
18
cli/list.go
@@ -2,10 +2,8 @@ package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/emeralt/npm-cache-proxy/proxy"
|
||||
"github.com/go-redis/redis"
|
||||
npmproxy "github.com/emeralt/npm-cache-proxy/proxy"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -14,15 +12,13 @@ var listCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List all cached packages",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
prx := proxy.Proxy{
|
||||
RedisClient: redis.NewClient(&redis.Options{}),
|
||||
HttpClient: &http.Client{
|
||||
Transport: http.DefaultTransport,
|
||||
},
|
||||
GetOptions: getOptions,
|
||||
}
|
||||
proxy := getProxy(func() (npmproxy.Options, error) {
|
||||
return npmproxy.Options{
|
||||
RedisPrefix: persistentOptions.RedisPrefix,
|
||||
}, nil
|
||||
})
|
||||
|
||||
metadatas, err := prx.ListMetadata()
|
||||
metadatas, err := proxy.ListMetadata()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
34
cli/main.go
34
cli/main.go
@@ -2,9 +2,43 @@ package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
npmproxy "github.com/emeralt/npm-cache-proxy/proxy"
|
||||
"github.com/go-redis/redis"
|
||||
)
|
||||
|
||||
// global options
|
||||
var persistentOptions struct {
|
||||
RedisAddress string
|
||||
RedisDatabase int
|
||||
RedisPassword string
|
||||
RedisPrefix string
|
||||
}
|
||||
|
||||
// initialize global options
|
||||
func init() {
|
||||
rootCmd.PersistentFlags().StringVar(&persistentOptions.RedisAddress, "redis-address", getEnvString("REDIS_ADDRESS", "localhost:6379"), "Redis address")
|
||||
rootCmd.PersistentFlags().IntVar(&persistentOptions.RedisDatabase, "redis-database", getEnvInt("REDIS_DATABASE", "0"), "Redis database")
|
||||
rootCmd.PersistentFlags().StringVar(&persistentOptions.RedisPassword, "redis-password", getEnvString("REDIS_PASSWORD", ""), "Redis password")
|
||||
rootCmd.PersistentFlags().StringVar(&persistentOptions.RedisPrefix, "redis-prefix", getEnvString("REDIS_PREFIX", "ncp-"), "Redis prefix")
|
||||
}
|
||||
|
||||
func getProxy(getOptions func() (npmproxy.Options, error)) *npmproxy.Proxy {
|
||||
return &npmproxy.Proxy{
|
||||
RedisClient: redis.NewClient(&redis.Options{
|
||||
Addr: persistentOptions.RedisAddress,
|
||||
DB: persistentOptions.RedisDatabase,
|
||||
Password: persistentOptions.RedisPassword,
|
||||
}),
|
||||
HttpClient: &http.Client{
|
||||
Transport: http.DefaultTransport,
|
||||
},
|
||||
GetOptions: getOptions,
|
||||
}
|
||||
}
|
||||
|
||||
// Run starts the CLI
|
||||
func Run() {
|
||||
rootCmd.AddCommand(listCmd)
|
||||
|
||||
15
cli/purge.go
15
cli/purge.go
@@ -1,10 +1,7 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
npmproxy "github.com/emeralt/npm-cache-proxy/proxy"
|
||||
"github.com/go-redis/redis"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -13,13 +10,11 @@ var purgeCmd = &cobra.Command{
|
||||
Use: "purge",
|
||||
Short: "Purge all cached packages",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
proxy := npmproxy.Proxy{
|
||||
RedisClient: redis.NewClient(&redis.Options{}),
|
||||
HttpClient: &http.Client{
|
||||
Transport: http.DefaultTransport,
|
||||
},
|
||||
GetOptions: getOptions,
|
||||
}
|
||||
proxy := getProxy(func() (npmproxy.Options, error) {
|
||||
return npmproxy.Options{
|
||||
RedisPrefix: persistentOptions.RedisPrefix,
|
||||
}, nil
|
||||
})
|
||||
|
||||
err := proxy.PurgeMetadata()
|
||||
if err != nil {
|
||||
|
||||
51
cli/root.go
51
cli/root.go
@@ -1,11 +1,9 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
npmproxy "github.com/emeralt/npm-cache-proxy/proxy"
|
||||
"github.com/go-redis/redis"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -13,28 +11,33 @@ import (
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "ncp",
|
||||
Short: "ncp is a fast npm cache proxy that stores data in Redis",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
proxy := npmproxy.Proxy{
|
||||
RedisClient: redis.NewClient(&redis.Options{}),
|
||||
HttpClient: &http.Client{
|
||||
Transport: http.DefaultTransport,
|
||||
},
|
||||
GetOptions: getOptions,
|
||||
}
|
||||
|
||||
proxy.Server(npmproxy.ServerOptions{
|
||||
ListenAddress: "localhost:8080",
|
||||
}).ListenAndServe()
|
||||
},
|
||||
Run: run,
|
||||
}
|
||||
|
||||
func getOptions() (npmproxy.Options, error) {
|
||||
return npmproxy.Options{
|
||||
RedisPrefix: "ncp-",
|
||||
RedisExpireTimeout: 1 * time.Hour,
|
||||
|
||||
UpstreamAddress: "http://registry.npmjs.org",
|
||||
ReplaceAddress: "https://registry.npmjs.org",
|
||||
StaticServerAddress: "http://localhost:8080",
|
||||
}, nil
|
||||
var rootOptions struct {
|
||||
ListenAddress string
|
||||
UpstreamAddress string
|
||||
CacheLimit string
|
||||
CacheTTL int
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.Flags().StringVar(&rootOptions.ListenAddress, "listen", getEnvString("LISTEN_ADDRESS", "localhost:8080"), "Address to listen")
|
||||
rootCmd.Flags().StringVar(&rootOptions.UpstreamAddress, "upstream", getEnvString("UPSTREAM_ADDRESS", "https://registry.npmjs.org"), "Upstream registry address")
|
||||
rootCmd.Flags().StringVar(&rootOptions.CacheLimit, "cache-limit", getEnvString("CACHE_LIMIT", "0"), "Cached packages count limit")
|
||||
rootCmd.Flags().IntVar(&rootOptions.CacheTTL, "cache-ttl", getEnvInt("CACHE_TTL", "3600"), "Cache expiration timeout in seconds")
|
||||
}
|
||||
|
||||
func run(cmd *cobra.Command, args []string) {
|
||||
proxy := getProxy(func() (npmproxy.Options, error) {
|
||||
return npmproxy.Options{
|
||||
RedisPrefix: persistentOptions.RedisPrefix,
|
||||
RedisExpireTimeout: time.Duration(rootOptions.CacheTTL) * time.Second,
|
||||
UpstreamAddress: rootOptions.UpstreamAddress,
|
||||
}, nil
|
||||
})
|
||||
|
||||
proxy.Server(npmproxy.ServerOptions{
|
||||
ListenAddress: rootOptions.ListenAddress,
|
||||
}).ListenAndServe()
|
||||
}
|
||||
|
||||
25
cli/utils.go
Normal file
25
cli/utils.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func getEnvString(env string, def string) string {
|
||||
value := os.Getenv(env)
|
||||
|
||||
if value != "" {
|
||||
return value
|
||||
} else {
|
||||
return def
|
||||
}
|
||||
}
|
||||
|
||||
func getEnvInt(env string, def string) int {
|
||||
value := getEnvString(env, def)
|
||||
|
||||
// TODO: handle error
|
||||
converted, _ := strconv.Atoi(value)
|
||||
|
||||
return converted
|
||||
}
|
||||
Reference in New Issue
Block a user