代码拉取完成,页面将自动刷新
同步操作将从 caixw/apidoc 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
// Copyright 2015 by caixw, All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
// apidoc 是一个 RESTful API 文档生成工具。
package main
import (
"bytes"
"flag"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"strings"
"github.com/issue9/logs/writers"
"github.com/issue9/term/colors"
"github.com/issue9/version"
yaml "gopkg.in/yaml.v2"
"github.com/caixw/apidoc/input"
"github.com/caixw/apidoc/locale"
"github.com/caixw/apidoc/output"
"github.com/caixw/apidoc/vars"
)
// 日志信息输出
var (
info = newLog(os.Stdout, vars.InfoColor, "[INFO] ")
warn = newLog(os.Stderr, vars.WarnColor, "[WARN] ")
erro = newLog(os.Stderr, vars.ErroColor, "[ERRO] ")
)
// 确保第一时间初始化本地化信息
func init() {
if err := locale.Init(); err != nil {
warn.Println(err)
return
}
info.SetPrefix(locale.Sprintf(locale.InfoPrefix))
warn.SetPrefix(locale.Sprintf(locale.WarnPrefix))
erro.SetPrefix(locale.Sprintf(locale.ErrorPrefix))
}
func main() {
h := flag.Bool("h", false, locale.Sprintf(locale.FlagHUsage))
v := flag.Bool("v", false, locale.Sprintf(locale.FlagVUsage))
g := flag.Bool("g", false, locale.Sprintf(locale.FlagGUsage))
wd := flag.String("wd", "./", locale.Sprintf(locale.FlagWDUsage))
languages := flag.Bool("languages", false, locale.Sprintf(locale.FlagLanguagesUsage))
encodings := flag.Bool("encodings", false, locale.Sprintf(locale.FlagEncodingsUsage))
pprofType := flag.String("pprof", "", locale.Sprintf(locale.FlagPprofUsage, vars.PprofCPU, vars.PprofMem))
flag.Usage = usage
flag.Parse()
switch {
case *h:
flag.Usage()
return
case *v:
printVersion()
return
case *languages:
locale.Printf(locale.FlagSupportedLanguages, input.Languages())
return
case *encodings:
locale.Printf(locale.FlagSupportedEncodings, input.Encodings())
return
case *g:
genConfigFile(*wd)
return
}
if len(*pprofType) > 0 {
buf := new(bytes.Buffer)
defer func() { // 在程序结束时,将内容写入到文件
profile := filepath.Join(*wd, *pprofType+".prof")
if err := ioutil.WriteFile(profile, buf.Bytes(), os.ModePerm); err != nil {
erro.Println(err)
}
}()
switch strings.ToLower(*pprofType) {
case vars.PprofMem:
defer func() {
if err := pprof.Lookup("heap").WriteTo(buf, 1); err != nil {
erro.Println(err)
}
}()
case vars.PprofCPU:
if err := pprof.StartCPUProfile(buf); err != nil {
erro.Println(err)
}
defer pprof.StopCPUProfile()
default:
erro.Println(locale.Sprintf(locale.FlagInvalidPprrof))
return
}
}
run(*wd)
}
// 真正的程序入口,main 主要是作参数的处理。
func run(wd string) {
cfg, err := loadConfig(filepath.Join(wd, vars.ConfigFilename))
if err != nil {
erro.Println(err)
return
}
// 比较版本号兼容问题
compatible, err := version.SemVerCompatible(vars.Version(), cfg.Version)
if err != nil {
erro.Println(err)
return
}
if !compatible {
erro.Println(locale.Sprintf(locale.VersionInCompatible))
return
}
docs, elapsed := input.Parse(cfg.Inputs...)
cfg.Output.Elapsed = elapsed
if err := output.Render(docs, cfg.Output); err != nil {
erro.Println(err)
return
}
info.Println(locale.Sprintf(locale.Complete, cfg.Output.Dir, elapsed))
}
func usage() {
buf := new(bytes.Buffer)
flag.CommandLine.SetOutput(buf)
flag.PrintDefaults()
locale.Printf(locale.FlagUsage, vars.Name, buf.String(), vars.RepoURL, vars.OfficialURL)
}
// 根据 wd 所在目录的内容生成一个配置文件,并写入到 wd 目录下的 .apidoc.yaml 中
func genConfigFile(wd string) {
o, err := input.Detect(wd, true)
if err != nil {
erro.Println(err)
return
}
cfg := &config{
Version: vars.Version(),
Inputs: []*input.Options{o},
Output: &output.Options{
Dir: filepath.Join(o.Dir, "doc"),
},
}
data, err := yaml.Marshal(cfg)
if err != nil {
erro.Println(err)
return
}
path := filepath.Join(wd, vars.ConfigFilename)
if err = ioutil.WriteFile(path, data, os.ModePerm); err != nil {
erro.Println(err)
return
}
info.Println(locale.Sprintf(locale.FlagConfigWritedSuccess, path))
}
func printVersion() {
locale.Printf(locale.FlagVersionBuildWith, vars.Name, vars.Version(), runtime.Version())
locale.Printf(locale.FlagVersionCommitHash, vars.CommitHash())
}
func newLog(out *os.File, color colors.Color, prefix string) *log.Logger {
return log.New(writers.NewConsole(out, color, colors.Default), prefix, 0)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。