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

Add command ndiag fetch-icons #53

Merged
merged 7 commits into from
Jan 9, 2021
Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ ndiag
dist/
doc/
archdoc/
./ndiag.descriptions/
ndiag.descriptions/
ndiag.icons/
coverage.txt
*-packr.go
.envrc
Expand Down
67 changes: 67 additions & 0 deletions cmd/fetchIcons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright © 2021 Ken'ichiro Oyama <k1lowxb@gmail.com>

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.
*/
package cmd

import (
"github.com/k1LoW/ndiag/icon"
"github.com/k1LoW/ndiag/icon/aws"
"github.com/k1LoW/ndiag/icon/gcp"
"github.com/k1LoW/ndiag/icon/k8s"
"github.com/spf13/cobra"
)

var fetchIconsCmd = &cobra.Command{
Use: "fetch-icons",
Short: "Fecth icon set from internet",
Long: `Fecth icon set from internet.`,
Args: cobra.OnlyValidArgs,
ValidArgs: []string{"aws", "gcp", "k8s"},
RunE: func(cmd *cobra.Command, args []string) error {
target := args[0]
var fetcher icon.Fetcher

cfg, err := newConfig()
if err != nil {
return err
}

switch target {
case "aws":
fetcher = &aws.AWSIcon{}
case "gcp":
fetcher = &gcp.GCPIcon{}
case "k8s":
fetcher = &k8s.K8sIcon{}
}

if iconPrefix == "" {
iconPrefix = target
}
return fetcher.Fetch(cfg.IconPath, iconPrefix)
},
}

func init() {
rootCmd.AddCommand(fetchIconsCmd)
fetchIconsCmd.Flags().StringVarP(&configPath, "config", "c", "", "config file path")
fetchIconsCmd.Flags().StringVarP(&iconPrefix, "prefix", "", "", "icon key prefix")
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
configPath string
out string
rmDist bool
iconPrefix string
)

var rootCmd = &cobra.Command{
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ require (
github.com/olekukonko/tablewriter v0.0.4
github.com/pasztorpisti/qs v0.0.0-20171216220353-8d6c33ee906c
github.com/spf13/cobra v1.1.1
github.com/stoewer/go-strcase v1.2.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,8 @@ github.com/srwiley/oksvg v0.0.0-20200311192757-870daf9aa564 h1:HunZiaEKNGVdhTRQO
github.com/srwiley/oksvg v0.0.0-20200311192757-870daf9aa564/go.mod h1:afMbS0qvv1m5tfENCwnOdZGOF8RGR/FsZ7bvBxQGZG4=
github.com/srwiley/rasterx v0.0.0-20200120212402-85cb7272f5e9 h1:m59mIOBO4kfcNCEzJNy71UkeF4XIx2EVmL9KLwDQdmM=
github.com/srwiley/rasterx v0.0.0-20200120212402-85cb7272f5e9/go.mod h1:mvWM0+15UqyrFKqdRjY6LuAVJR0HOVhJlEgZ5JWtSWU=
github.com/stoewer/go-strcase v1.2.0 h1:Z2iHWqGXH00XYgqDmNgQbIBxf3wrNq0F3feEy0ainaU=
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
81 changes: 81 additions & 0 deletions icon/aws/aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package aws

import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/k1LoW/ndiag/icon"
"github.com/stoewer/go-strcase"
)

const archiveURL = "https://d1.awsstatic.com/webteam/architecture-icons/Q32020/AWS-Architecture-Assets-For-Light-and-Dark-BG_20200911.478ff05b80f909792f7853b1a28de8e28eac67f4.zip"

type AWSIcon struct{}

var rep = strings.NewReplacer("_Light", "", "_48", "", "loT", "iot", "IoT", "iot", "FSx", "fsx", "AMIs", "amis", "_", "-", "&", "and", "VMware", "vmware")
var rep2 = strings.NewReplacer("res-amazon", "res", "res-aws", "res", "arch-aws-", "", "arch-amazon-", "")

func (f *AWSIcon) Fetch(iconPath, prefix string) error {
_, _ = fmt.Fprintf(os.Stderr, "Fetching from %s ...\n", archiveURL)
dir, err := ioutil.TempDir("", "ndiag-icon-aws")
if err != nil {
return err
}
defer os.RemoveAll(dir)
ap, err := icon.Download(archiveURL, dir)
if err != nil {
return err
}
r, err := zip.OpenReader(ap)
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Join(iconPath, prefix), 0750); err != nil {
return err
}

for _, f := range r.File {
if strings.Contains(f.Name, "_Dark") {
continue
}
if strings.Contains(f.Name, "_64") || strings.Contains(f.Name, "_32") || strings.Contains(f.Name, "_16") {
continue
}
if !strings.Contains(f.Name, ".svg") {
continue
}
if f.FileInfo().IsDir() {
continue
}

rc, err := f.Open()
if err != nil {
return err
}
fn := rep2.Replace(strcase.KebabCase(rep.Replace(filepath.Base(f.Name))))

path := filepath.Join(iconPath, prefix, fn)

buf := make([]byte, f.UncompressedSize)
_, err = io.ReadFull(rc, buf)
if err != nil {
_ = rc.Close()
return err
}
if err := ioutil.WriteFile(path, buf, f.Mode()); err != nil {
_ = rc.Close()
return err
}
if err := rc.Close(); err != nil {
return err
}
}

_, _ = fmt.Fprintf(os.Stderr, "%s\n", "Done.")
return nil
}
46 changes: 46 additions & 0 deletions icon/fetcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package icon

import (
"io"
"net"
"net/http"
"os"
"path/filepath"
"time"
)

type Fetcher interface {
Fetch(iconPath, prefix string) error
}

func Download(src, dest string) (string, error) {
client := &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
},
}

resp, err := client.Get(src)
if err != nil {
return "", err
}
defer resp.Body.Close()

p := filepath.Join(dest, filepath.Base(src))
f, err := os.OpenFile(p, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return "", err
}
if _, err = io.Copy(f, resp.Body); err != nil {
_ = f.Close()
return "", err
}
if err := f.Close(); err != nil {
return "", err
}
return p, nil
}
78 changes: 78 additions & 0 deletions icon/gcp/gcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package gcp

import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/k1LoW/ndiag/icon"
"github.com/stoewer/go-strcase"
)

const archiveURL = "https://cloud.google.com/icons/files/google-cloud-icons.zip"

var pathRe = regexp.MustCompile(`\A.+/([^/]+)\.svg\z`)
var rep = strings.NewReplacer("-512-color", "", "-521-color", "", "-color", "", " (1)", "", "_", "-")

type GCPIcon struct{}

func (f *GCPIcon) Fetch(iconPath, prefix string) error {
_, _ = fmt.Fprintf(os.Stderr, "Fetching from %s ...\n", archiveURL)
dir, err := ioutil.TempDir("", "ndiag-icon-gcp")
if err != nil {
return err
}
defer os.RemoveAll(dir)
ap, err := icon.Download(archiveURL, dir)
if err != nil {
return err
}
r, err := zip.OpenReader(ap)
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Join(iconPath, prefix), 0750); err != nil {
return err
}

for _, f := range r.File {
if strings.Contains(f.Name, "__MACOSX") {
continue
}
if strings.Contains(f.Name, "Expanded Product Card Icons") {
continue
}
if f.FileInfo().IsDir() {
continue
}
matched := pathRe.FindStringSubmatch(f.Name)
if len(matched) == 0 {
continue
}
rc, err := f.Open()
if err != nil {
return err
}
buf := make([]byte, f.UncompressedSize)
_, err = io.ReadFull(rc, buf)
if err != nil {
_ = rc.Close()
return err
}
path := filepath.Join(iconPath, prefix, fmt.Sprintf("%s.%s", strcase.KebabCase(rep.Replace(matched[1])), "svg"))
if err := ioutil.WriteFile(path, buf, f.Mode()); err != nil {
_ = rc.Close()
return err
}
if err := rc.Close(); err != nil {
return err
}
}
_, _ = fmt.Fprintf(os.Stderr, "%s\n", "Done.")
return nil
}
79 changes: 79 additions & 0 deletions icon/k8s/k8s.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package k8s

import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/k1LoW/ndiag/icon"
)

const archiveURL = "https://github.com/kubernetes/community/archive/master.zip"

var pathRe = regexp.MustCompile(`\A.+/([^/]+)/([^/]+)/([^/]+)\.svg\z`)
var rep = strings.NewReplacer("control_plane_components", "control-plane", "infrastructure_components", "infra", "_", "-")

type K8sIcon struct{}

func (f *K8sIcon) Fetch(iconPath, prefix string) error {
_, _ = fmt.Fprintf(os.Stderr, "Fetching from %s ...\n", archiveURL)
dir, err := ioutil.TempDir("", "ndiag-icon-k8s")
if err != nil {
return err
}
defer os.RemoveAll(dir)
ap, err := icon.Download(archiveURL, dir)
if err != nil {
return err
}
r, err := zip.OpenReader(ap)
if err != nil {
return err
}
for _, f := range r.File {
if !strings.Contains(f.Name, "icons/svg") {
continue
}
if f.FileInfo().IsDir() {
continue
}
matched := pathRe.FindStringSubmatch(f.Name)
if len(matched) == 0 {
continue
}

rc, err := f.Open()
if err != nil {
return err
}
buf := make([]byte, f.UncompressedSize)
_, err = io.ReadFull(rc, buf)
if err != nil {
_ = rc.Close()
return err
}
var path string
if matched[2] == "labeled" {
path = rep.Replace(filepath.Join(iconPath, prefix, matched[1], fmt.Sprintf("%s.%s", matched[3], "svg")))
} else {
path = rep.Replace(filepath.Join(iconPath, prefix, matched[1], matched[3], fmt.Sprintf("%s.%s", matched[2], "svg")))
}
if err := os.MkdirAll(filepath.Dir(path), 0750); err != nil {
return err
}
if err := ioutil.WriteFile(path, buf, f.Mode()); err != nil {
_ = rc.Close()
return err
}
if err := rc.Close(); err != nil {
return err
}
}
_, _ = fmt.Fprintf(os.Stderr, "%s\n", "Done.")
return nil
}