-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the basic replacer implementation (#1)
- Loading branch information
Showing
19 changed files
with
601 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# To get started with Dependabot version updates, you'll need to specify which | ||
# package ecosystems to update and where the package manifests are located. | ||
# Please see the documentation for all configuration options: | ||
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates | ||
|
||
version: 2 | ||
updates: | ||
- package-ecosystem: "gomod" | ||
directory: "/" | ||
schedule: | ||
interval: "weekly" | ||
- package-ecosystem: "github-actions" | ||
directory: "/" | ||
schedule: | ||
interval: "weekly" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# For most projects, this workflow file will not need changing; you simply need | ||
# to commit it to your repository. | ||
# | ||
# You may wish to alter this file to override the set of languages analyzed, | ||
# or to provide custom queries or build logic. | ||
# | ||
# ******** NOTE ******** | ||
# We have attempted to detect the languages in your repository. Please check | ||
# the `language` matrix defined below to confirm you have the correct set of | ||
# supported CodeQL languages. | ||
# | ||
name: "CodeQL" | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
# The branches below must be a subset of the branches above | ||
branches: [ "main" ] | ||
schedule: | ||
- cron: '0 18 * * 2' | ||
|
||
jobs: | ||
analyze: | ||
name: Analyze | ||
runs-on: ubuntu-latest | ||
permissions: | ||
actions: read | ||
contents: read | ||
security-events: write | ||
|
||
strategy: | ||
fail-fast: false | ||
matrix: | ||
language: [ 'go' ] | ||
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] | ||
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
# Initializes the CodeQL tools for scanning. | ||
- name: Initialize CodeQL | ||
uses: github/codeql-action/init@v2 | ||
with: | ||
languages: ${{ matrix.language }} | ||
# If you wish to specify custom queries, you can do so here or in a config file. | ||
# By default, queries listed here will override any specified in a config file. | ||
# Prefix the list here with "+" to use these queries and those in the config file. | ||
|
||
# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs | ||
# queries: security-extended,security-and-quality | ||
|
||
|
||
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java). | ||
# If this step fails, then you should remove it and run the build manually (see below) | ||
- name: Autobuild | ||
uses: github/codeql-action/autobuild@v2 | ||
|
||
# ℹ️ Command-line programs to run using the OS shell. | ||
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun | ||
|
||
# If the Autobuild fails above, remove it and uncomment the following three lines. | ||
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance. | ||
|
||
# - run: | | ||
# echo "Run, Build Application using script" | ||
# ./location_of_script_within_repo/buildscript.sh | ||
|
||
- name: Perform CodeQL Analysis | ||
uses: github/codeql-action/analyze@v2 | ||
with: | ||
category: "/language:${{matrix.language}}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# This workflow will build a golang project | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go | ||
|
||
name: Go | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
|
||
build: | ||
strategy: | ||
matrix: | ||
go: [ '1.21' ] | ||
os: [ 'ubuntu-latest', 'windows-latest', 'macos-latest' ] | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: ${{ matrix.go }} | ||
|
||
- name: Build | ||
run: go build -v ./... | ||
|
||
- name: Test | ||
run: go test -v -race ./... -coverprofile=coverage.txt -covermode=atomic -timeout=10m | ||
|
||
- name: Codecov | ||
uses: codecov/codecov-action@v3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,9 @@ | |
|
||
# Go workspace file | ||
go.work | ||
|
||
.idea/ | ||
.vscode/ | ||
bin/ | ||
.run/ | ||
logs/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,46 @@ | ||
# replacer | ||
# replacer | ||
|
||
[![Build](https://img.shields.io/github/actions/workflow/status/no-src/replacer/go.yml?branch=main)](https://github.com/no-src/replacer/actions) | ||
[![License](https://img.shields.io/github/license/no-src/replacer)](https://github.com/no-src/replacer/blob/main/LICENSE) | ||
[![Go Reference](https://pkg.go.dev/badge/github.com/no-src/replacer.svg)](https://pkg.go.dev/github.com/no-src/replacer) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/no-src/replacer)](https://goreportcard.com/report/github.com/no-src/replacer) | ||
[![codecov](https://codecov.io/gh/no-src/replacer/graph/badge.svg?token=aJZPSpxpK7)](https://codecov.io/gh/no-src/replacer) | ||
[![Release](https://img.shields.io/github/v/release/no-src/replacer)](https://github.com/no-src/replacer/releases) | ||
|
||
The replacer is a configuration-based file replace tool. | ||
|
||
## Installation | ||
|
||
The first need [Go](https://go.dev/doc/install) installed (**version 1.21+ is required**), then you can use the below | ||
command to install `replacer`. | ||
|
||
```bash | ||
go install github.com/no-src/replacer/...@latest | ||
``` | ||
|
||
## Quick Start | ||
|
||
Create a config file named `replacer.yaml`, content is as follows | ||
|
||
```yaml | ||
name: the configuration of replacer | ||
version: v0.0.1 | ||
items: | ||
- name: replace domain | ||
paths: | ||
- "*/test.yaml" | ||
rules: | ||
- old: | ||
- 127.0.0.1:8888 | ||
- 127.0.0.1:9999 | ||
new: | ||
test: test-github.com | ||
uat: uat-github.com | ||
product: product-github.com | ||
``` | ||
Now running the command below start to replace the files. | ||
```bash | ||
$ replacer -root="./testdata/testfile" -tag=test | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cmd | ||
|
||
import ( | ||
"flag" | ||
|
||
"github.com/no-src/log/level" | ||
) | ||
|
||
var ( | ||
ConfigFile string | ||
ConfigUrl string | ||
Root string | ||
Tag string | ||
LogDir string | ||
LogLevel int | ||
Revert bool | ||
) | ||
|
||
// InitFlags init built-in flags | ||
func InitFlags() { | ||
flag.StringVar(&ConfigFile, "conf", "./replacer.yaml", "the config file of replacer") | ||
flag.StringVar(&ConfigUrl, "conf_url", "", "the remote url of replacer config file") | ||
flag.StringVar(&Root, "root", "./", "workspace root path") | ||
flag.StringVar(&Tag, "tag", "default", "tag name") | ||
flag.StringVar(&LogDir, "log_dir", "./logs", "log directory") | ||
flag.IntVar(&LogLevel, "log_level", int(level.DebugLevel), "log level") | ||
flag.BoolVar(&Revert, "revert", false, "revert the replace operations") | ||
flag.Parse() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/no-src/log" | ||
"github.com/no-src/log/level" | ||
"github.com/no-src/nsgo/httputil" | ||
"github.com/no-src/replacer" | ||
"github.com/no-src/replacer/internal/logger" | ||
) | ||
|
||
// RunWithFlags start the replacer with built-in flags | ||
func RunWithFlags() error { | ||
return Run(ConfigFile, ConfigUrl, Root, Tag, LogDir, LogLevel, Revert) | ||
} | ||
|
||
// Run start the replacer with custom arguments | ||
func Run(conf, configUrl, root, tag, logDir string, logLevel int, revert bool) error { | ||
if err := logger.InitLogger("replacer", logDir, level.Level(logLevel)); err != nil { | ||
return err | ||
} | ||
if len(configUrl) > 0 { | ||
client, err := httputil.NewHttpClient(true, "", false) | ||
if err != nil { | ||
return err | ||
} | ||
if err = client.Download(conf, configUrl, false); err != nil { | ||
return err | ||
} | ||
} | ||
r, err := replacer.NewReplacer(conf) | ||
if err != nil { | ||
log.Error(err, "init replacer config error") | ||
return err | ||
} | ||
if revert { | ||
err = r.Revert(root, tag) | ||
} else { | ||
err = r.Replace(root, tag) | ||
} | ||
if err != nil { | ||
log.Error(err, "execute replace error") | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/no-src/log" | ||
"github.com/no-src/replacer/cmd" | ||
) | ||
|
||
func main() { | ||
var err error | ||
defer func() { | ||
log.Close() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
}() | ||
cmd.InitFlags() | ||
err = log.ErrorIf(cmd.RunWithFlags(), "running replacer error") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package conf | ||
|
||
// Conf the replacer config | ||
type Conf struct { | ||
Name string `yaml:"name"` | ||
Version string `yaml:"version"` | ||
Items []Item `json:"items"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package conf | ||
|
||
// Item the replacer config item | ||
type Item struct { | ||
Name string `yaml:"name"` | ||
Disabled bool `yaml:"disabled"` | ||
Paths []string `yaml:"paths"` | ||
Rules []Rule `yaml:"rules"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package conf | ||
|
||
// Rule the replacer rule | ||
type Rule struct { | ||
Name string `yaml:"name"` | ||
Old []string `yaml:"old"` | ||
New map[string]string `yaml:"new"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module github.com/no-src/replacer | ||
|
||
go 1.21 | ||
|
||
require ( | ||
github.com/no-src/log v0.3.1 | ||
github.com/no-src/nsgo v0.0.0-20231106032541-de0f7f19b7e2 | ||
) | ||
|
||
require ( | ||
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect | ||
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect | ||
github.com/kr/text v0.2.0 // indirect | ||
github.com/onsi/ginkgo/v2 v2.9.5 // indirect | ||
github.com/quic-go/qpack v0.4.0 // indirect | ||
github.com/quic-go/qtls-go1-20 v0.4.1 // indirect | ||
github.com/quic-go/quic-go v0.40.0 // indirect | ||
github.com/rogpeppe/go-internal v1.11.0 // indirect | ||
go.uber.org/mock v0.3.0 // indirect | ||
golang.org/x/crypto v0.13.0 // indirect | ||
golang.org/x/exp v0.0.0-20230210204819-062eb4c674ab // indirect | ||
golang.org/x/mod v0.11.0 // indirect | ||
golang.org/x/net v0.15.0 // indirect | ||
golang.org/x/sys v0.12.0 // indirect | ||
golang.org/x/text v0.13.0 // indirect | ||
golang.org/x/tools v0.9.1 // indirect | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
Oops, something went wrong.