Skip to content

Commit

Permalink
isutoolsより分離
Browse files Browse the repository at this point in the history
  • Loading branch information
mazrean committed Nov 23, 2023
1 parent 33f018f commit 3b77b69
Show file tree
Hide file tree
Showing 19 changed files with 1,371 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: 2
updates:
- package-ecosystem: gomod
directory: "/"
schedule:
interval: weekly
day: saturday
time: "00:00"
timezone: Asia/Tokyo
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: weekly
day: saturday
time: "00:00"
timezone: Asia/Tokyo
58 changes: 58 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: CI

on:
push:
branches:
- "main"
pull_request:

jobs:
build:
name: Build
runs-on: ubuntu-latest
env:
GOCACHE: "/tmp/go/cache"
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version-file: go.mod
cache: true
- uses: actions/cache@v3
with:
path: /tmp/go/cache
key: ${{ runner.os }}-go-build-${{ github.ref }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-go-build-${{ github.ref }}-
${{ runner.os }}-go-build-
- run: go build -o isucrud .
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version-file: go.mod
cache: true
- run: go test ./... -v -coverprofile=./coverage.txt -race -vet=off
- name: Upload coverage data
uses: codecov/[email protected]
with:
file: ./coverage.txt
- uses: actions/upload-artifact@v3
with:
name: coverage.txt
path: coverage.txt
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: golangci-lint
uses: reviewdog/[email protected]
with:
go_version_file: go.mod
reporter: github-pr-check
github_token: ${{ secrets.GITHUB_TOKEN }}
fail_on_error: true
34 changes: 34 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Release

on:
push:
tags:
- "v*"

env:
APP_NAME: isucrud

jobs:
build:
name: Release
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-go@v4
with:
go-version-file: go.mod
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload assets
uses: actions/upload-artifact@v3
with:
name: assets
path: ./dist/*
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
41 changes: 41 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
before:
hooks:
- go mod download
builds:
- env:
- CGO_ENABLED=0
ldflags:
- -s
- -w
- -X main.version={{.Version}}
- -X main.revision={{.ShortCommit}}
goos:
- linux
- windows
- darwin
main: ./

archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of uname.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
# use zip for windows archives
format_overrides:
- goos: windows
format: zip
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ incpatch .Version }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
14 changes: 14 additions & 0 deletions flagType.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import "fmt"

type sliceString []string

func (ss *sliceString) String() string {
return fmt.Sprintf("%s", *ss)
}

func (ss *sliceString) Set(value string) error {
*ss = append(*ss, value)
return nil
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/mazrean/isucrud

go 1.21.3

require golang.org/x/tools v0.15.0

require (
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sys v0.14.0 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.15.0 h1:zdAyfUGbYmuVokhzVmghFl2ZJh5QhcfebBgmVPFYA+8=
golang.org/x/tools v0.15.0/go.mod h1:hpksKq4dtpQWS1uQ61JkdqWM3LscIS6Slf+VVkm+wQk=
66 changes: 66 additions & 0 deletions internal/dbdoc/dbdoc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package dbdoc

import (
"fmt"
"go/token"
"os"

"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
)

type Config struct {
WorkDir string
BuildArgs []string
IgnoreFuncs []string
IgnoreFuncPrefixes []string
DestinationFilePath string
}

func Run(conf Config) error {
ctx := &context{
fileSet: token.NewFileSet(),
workDir: conf.WorkDir,
}

ssaProgram, pkgs, err := buildSSA(ctx, conf.BuildArgs)
if err != nil {
return fmt.Errorf("failed to build ssa: %w", err)
}

funcs, err := buildFuncs(ctx, pkgs, ssaProgram)
if err != nil {
return fmt.Errorf("failed to build funcs: %w", err)
}

nodes := buildGraph(funcs, conf.IgnoreFuncs, conf.IgnoreFuncPrefixes)

f, err := os.Create(conf.DestinationFilePath)
if err != nil {
return fmt.Errorf("failed to make directory: %w", err)
}
defer f.Close()

err = writeMermaid(f, nodes)
if err != nil {
return fmt.Errorf("failed to write mermaid: %w", err)
}

return nil
}

func buildSSA(ctx *context, args []string) (*ssa.Program, []*packages.Package, error) {
pkgs, err := packages.Load(&packages.Config{
Fset: ctx.fileSet,
Mode: packages.NeedFiles | packages.NeedSyntax | packages.NeedTypes | packages.NeedImports | packages.NeedTypesInfo | packages.NeedName | packages.NeedModule,
}, args...)
if err != nil {
return nil, nil, fmt.Errorf("failed to load packages: %w", err)
}

ssaProgram, _ := ssautil.AllPackages(pkgs, ssa.BareInits)
ssaProgram.Build()

return ssaProgram, pkgs, nil
}
Loading

0 comments on commit 3b77b69

Please sign in to comment.