feat: add cli - start server, list packages, purge packages
This commit is contained in:
34
cli/list.go
Normal file
34
cli/list.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/emeralt/npm-cache-proxy/proxy"
|
||||
"github.com/go-redis/redis"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// start a server
|
||||
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,
|
||||
}
|
||||
|
||||
metadatas, err := prx.ListMetadata()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, metadata := range metadatas {
|
||||
fmt.Println(metadata)
|
||||
}
|
||||
},
|
||||
}
|
||||
17
cli/main.go
Normal file
17
cli/main.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Run starts the CLI
|
||||
func Run() {
|
||||
rootCmd.AddCommand(listCmd)
|
||||
rootCmd.AddCommand(purgeCmd)
|
||||
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
29
cli/purge.go
Normal file
29
cli/purge.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
npmproxy "github.com/emeralt/npm-cache-proxy/proxy"
|
||||
"github.com/go-redis/redis"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// start a server
|
||||
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,
|
||||
}
|
||||
|
||||
err := proxy.PurgeMetadata()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
40
cli/root.go
Normal file
40
cli/root.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
npmproxy "github.com/emeralt/npm-cache-proxy/proxy"
|
||||
"github.com/go-redis/redis"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// start a server
|
||||
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()
|
||||
},
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user