func needCheckUpdate() bool { // Does not have record for check update. stamp, err := Cfg.Int64("app", "update_check_time") if err != nil { return true } if !com.IsFile("conf/docTree.json") || !com.IsFile("conf/blogTree.json") { return true } return time.Unix(stamp, 0).Add(5 * time.Minute).Before(time.Now()) }
// copy static assets func (b *Builder) copyAssets(ctx *Context, srcDir string, dstDir string) error { return filepath.Walk(srcDir, func(p string, fi os.FileInfo, err error) error { if err != nil { return err } if fi.IsDir() { return nil } // if file exist, check mod time rel, _ := filepath.Rel(srcDir, p) toFile := filepath.Join(dstDir, rel) if com.IsFile(toFile) { if fi2, _ := os.Stat(toFile); fi2 != nil { if fi2.ModTime().Sub(fi.ModTime()).Seconds() > 0 { ctx.Diff.Add(toFile, DIFF_KEEP, fi2.ModTime()) return nil } if err := helper.CopyFile(p, toFile); err != nil { return err } ctx.Diff.Add(toFile, DIFF_UPDATE, time.Now()) return nil } } // not exist, just copy if err := helper.CopyFile(p, toFile); err != nil { return err } ctx.Diff.Add(toFile, DIFF_ADD, time.Now()) return nil }) }
func LoadPkgNameList(filePath string) { PackageNameList = make(map[string]string) // If file does not exist, simply ignore. if !com.IsFile(filePath) { return } data, err := ioutil.ReadFile(filePath) if err != nil { log.Error("Package name list", "Fail to read file") log.Fatal("", err.Error()) } pkgs := strings.Split(string(data), "\n") for i, line := range pkgs { infos := strings.Split(line, "=") if len(infos) != 2 { // Last item might be empty line. if i == len(pkgs)-1 { continue } log.Error("", "Fail to parse package name: "+line) log.Fatal("", "Invalid package name information") } PackageNameList[strings.TrimSpace(infos[0])] = strings.TrimSpace(infos[1]) } }
func TarGzDownload(ctx *middleware.Context, params martini.Params) { commitId := ctx.Repo.CommitId archivesPath := filepath.Join(ctx.Repo.GitRepo.Path, "archives/targz") if !com.IsDir(archivesPath) { if err := os.MkdirAll(archivesPath, 0755); err != nil { ctx.Handle(404, "TarGzDownload -> os.Mkdir(archivesPath)", err) return } } archivePath := filepath.Join(archivesPath, commitId+".tar.gz") if com.IsFile(archivePath) { ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+".tar.gz") return } err := ctx.Repo.Commit.CreateArchive(archivePath, git.AT_TARGZ) if err != nil { ctx.Handle(404, "TarGzDownload -> CreateArchive "+archivePath, err) return } ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+".tar.gz") }
func Pages(ctx *middleware.Context) { toc := models.Tocs[ctx.Locale.Language()] if toc == nil { toc = models.Tocs[setting.Docs.Langs[0]] } pageName := strings.ToLower(strings.TrimSuffix(ctx.Req.URL.Path[1:], ".html")) for i := range toc.Pages { if toc.Pages[i].Name == pageName { page := toc.Pages[i] if !com.IsFile(page.FileName) { ctx.Data["IsShowingDefault"] = true page = models.Tocs[setting.Docs.Langs[0]].Pages[i] } if !setting.ProdMode { page.ReloadContent() } ctx.Data["Title"] = page.Title ctx.Data["Content"] = fmt.Sprintf(`<script type="text/javascript" src="/%s/%s?=%d"></script>`, toc.Lang, page.DocumentPath+".js", page.LastBuildTime) ctx.Data["Pages"] = toc.Pages renderEditPage(ctx, page.DocumentPath) ctx.HTML(200, "docs") return } } NotFound(ctx) }
func (p *FileProvider) regenerate(oldsid, sid string) (err error) { p.lock.Lock() defer p.lock.Unlock() filename := p.filepath(sid) if com.IsExist(filename) { return fmt.Errorf("new sid '%s' already exists", sid) } oldname := p.filepath(oldsid) if !com.IsFile(oldname) { data, err := EncodeGob(make(map[interface{}]interface{})) if err != nil { return err } if err = os.MkdirAll(path.Dir(oldname), os.ModePerm); err != nil { return err } if err = ioutil.WriteFile(oldname, data, os.ModePerm); err != nil { return err } } if err = os.MkdirAll(path.Dir(filename), os.ModePerm); err != nil { return err } if err = os.Rename(oldname, filename); err != nil { return err } return nil }
func InitSetting() { var err error WorkDir, err = os.Getwd() if err != nil { log.Fatal("Fail to get work directory: %v", err) } confPath := path.Join(WorkDir, ".bra.toml") if !com.IsFile(confPath) { log.Fatal(".bra.toml not found in work directory") } else if _, err = toml.DecodeFile(confPath, &Cfg); err != nil { log.Fatal("Fail to decode .bra.toml: %v", err) } if Cfg.Run.InterruptTimeout == 0 { Cfg.Run.InterruptTimeout = 15 } // Init default ignore lists. Cfg.Run.IgnoreDirs = com.AppendStr(Cfg.Run.IgnoreDirs, ".git") Cfg.Run.IgnoreRegexps = make([]*regexp.Regexp, len(Cfg.Run.IgnoreFiles)) for i, regStr := range Cfg.Run.IgnoreFiles { Cfg.Run.IgnoreRegexps[i], err = regexp.Compile(regStr) if err != nil { log.Fatal("Invalid regexp[%s]: %v", regStr, err) } } }
func ExampleIsFile() { if com.IsFile("file.go") { fmt.Println("file.go exists") return } fmt.Println("file.go is not a file or does not exist") }
func TestRegister(t *testing.T) { e := s.Register(new(School), 55) if e != nil { t.Error(e) return } name := strings.ToLower(fmt.Sprint(reflect.TypeOf(new(School)).Elem())) sc := s.schema[name] if sc == nil { printError(t, "RegisterNoSchema", name, nil) return } if sc.PK != "Id" { printError(t, "RegisterPk", "Id", sc.PK) return } if sc.Index[0] != "Rank" { printError(t, "RegisterIndexHas", "Rank", sc.Index[1]) return } file := path.Join(directory, name, "rank.idx") if !com.IsFile(file) { printError(t, "RegisterHasFile:"+file, true, false) return } if sc.ChunkSize != 55 { printError(t, "RegisterChunkSize", 55, sc.ChunkSize) } }
func DeleteUploads(uploads ...*Upload) (err error) { if len(uploads) == 0 { return nil } sess := x.NewSession() defer sessionRelease(sess) if err = sess.Begin(); err != nil { return err } ids := make([]int64, len(uploads)) for i := 0; i < len(uploads); i++ { ids[i] = uploads[i].ID } if _, err = sess.In("id", ids).Delete(new(Upload)); err != nil { return fmt.Errorf("delete uploads: %v", err) } for _, upload := range uploads { localPath := upload.LocalPath() if !com.IsFile(localPath) { continue } if err := os.Remove(localPath); err != nil { return fmt.Errorf("remove upload: %v", err) } } return sess.Commit() }
func init() { var err error Cfg, err = ini.Load(CFG_PATH) if err != nil { panic(fmt.Errorf("fail to load config file '%s': %v", CFG_PATH, err)) } if com.IsFile(CFG_CUSTOM_PATH) { if err = Cfg.Append(CFG_CUSTOM_PATH); err != nil { panic(fmt.Errorf("fail to load config file '%s': %v", CFG_CUSTOM_PATH, err)) } } appNames := Cfg.Section("").Key("apps").Strings(",") Apps = make([]App, len(appNames)) for i, name := range appNames { Apps[i] = App{name, Cfg.Section(name).Key("repo_name").String()} } sec := Cfg.Section("app") if sec.Key("run_mode").MustString("dev") == "prod" { macaron.Env = macaron.PROD } HttpPort = sec.Key("http_port").MustInt(8091) Https = sec.Key("https").MustBool() HttpsCert = sec.Key("https_cert").String() HttpsKey = sec.Key("https_key").String() Langs = Cfg.Section("i18n").Key("langs").Strings(",") Names = Cfg.Section("i18n").Key("names").Strings(",") setGithubCredentials(Cfg.Section("github").Key("client_id").String(), Cfg.Section("github").Key("client_secret").String()) }
// get current theme func GetCurrentTheme() (*Theme, error) { themeSetting, err := GetSettings("theme") if err != nil { return nil, err } t := new(Theme) t.Directory = themeSetting["theme"] t.IsCurrent = true tomlFile := path.Join("static", t.Directory, "theme.toml") if com.IsFile(tomlFile) { if _, err := toml.DecodeFile(tomlFile, t); err != nil { log.Error("Db|GetCurrentTheme|%s|%s", tomlFile, err.Error()) return nil, err } } // fill data if t.Name == "" { t.Name = t.Directory } if t.Version == "" { t.Version = "0.0" } return t, nil }
func Download(ctx *middleware.Context) { ext := "." + ctx.Params(":ext") var archivePath string switch ext { case ".zip": archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip") case ".tar.gz": archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz") default: ctx.Error(404) return } if !com.IsDir(archivePath) { if err := os.MkdirAll(archivePath, os.ModePerm); err != nil { ctx.Handle(500, "Download -> os.MkdirAll(archivePath)", err) return } } archivePath = path.Join(archivePath, ctx.Repo.CommitId+ext) if !com.IsFile(archivePath) { if err := ctx.Repo.Commit.CreateArchive(archivePath, git.ZIP); err != nil { ctx.Handle(500, "Download -> CreateArchive "+archivePath, err) return } } ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+base.ShortSha(ctx.Repo.CommitId)+ext) }
func makeLink(srcPath, destPath string) error { // Check if Windows version is XP. if getWindowsVersion() >= 6 { cmd := exec.Command("cmd", "/c", "mklink", "/j", destPath, srcPath) return cmd.Run() } // XP. isWindowsXP = true // if both are ntfs file system if volumnType(srcPath) == "NTFS" && volumnType(destPath) == "NTFS" { // if has junction command installed file, err := exec.LookPath("junction") if err == nil { path, _ := filepath.Abs(file) if com.IsFile(path) { cmd := exec.Command("cmd", "/c", "junction", destPath, srcPath) return cmd.Run() } } } os.RemoveAll(destPath) return com.CopyDir(srcPath, destPath, func(filePath string) bool { return strings.Contains(filePath, doc.VENDOR) }) }
// validPath checks if the information of the package is valid. func validPath(info string) (string, string) { infos := strings.Split(info, ":") l := len(infos) switch { case l == 1: // for local imports if com.IsFile(infos[0]) { return doc.LOCAL, infos[0] } return doc.BRANCH, "" case l == 2: switch infos[1] { case doc.TRUNK, doc.MASTER, doc.DEFAULT: infos[1] = "" } return infos[0], infos[1] default: log.Error("", "Cannot parse dependency version:") log.Error("", "\t"+info) log.Help("Try 'gopm help get' to get more information") return "", "" } }
func InitModels() { if !com.IsFile(_CFG_PATH) { os.Create(_CFG_PATH) } var err error Cfg, err = goconfig.LoadConfigFile(_CFG_PATH) if err == nil { beego.Info("Initialize app.ini") } setGithubCredentials(Cfg.MustValue("github", "client_id"), Cfg.MustValue("github", "client_secret")) docLock = new(sync.RWMutex) blogLock = new(sync.RWMutex) initMaps() // Start check ticker. checkTicker = time.NewTicker(5 * time.Minute) go checkTickerTimer(checkTicker.C) // ATTENTION: you'd better comment following code when developing. if needCheckUpdate() { checkFileUpdates() Cfg.SetValue("app", "update_check_time", strconv.Itoa(int(time.Now().Unix()))) goconfig.SaveConfigFile(Cfg, _CFG_PATH) } }
func NewConfigContext() { workDir, err := ExecDir() if err != nil { qlog.Fatalf("Fail to get work directory: %s\n", err) } cfgPath := filepath.Join(workDir, "conf/app.ini") Cfg, err = goconfig.LoadConfigFile(cfgPath) if err != nil { qlog.Fatalf("Cannot load config file(%s): %v\n", cfgPath, err) } Cfg.BlockMode = false cfgPaths := []string{os.Getenv("GOGS_CONFIG"), filepath.Join(workDir, "custom/conf/app.ini")} for _, cfgPath := range cfgPaths { if com.IsFile(cfgPath) { if err = Cfg.AppendFiles(cfgPath); err != nil { qlog.Fatalf("Cannot load config file(%s): %v\n", cfgPath, err) } } } AppName = Cfg.MustValue("", "APP_NAME", "Gogs: Go Git Service") AppLogo = Cfg.MustValue("", "APP_LOGO", "img/favicon.png") AppUrl = Cfg.MustValue("server", "ROOT_URL") Domain = Cfg.MustValue("server", "DOMAIN") OfflineMode = Cfg.MustBool("server", "OFFLINE_MODE", false) DisableRouterLog = Cfg.MustBool("server", "DISABLE_ROUTER_LOG", false) SecretKey = Cfg.MustValue("security", "SECRET_KEY") InstallLock = Cfg.MustBool("security", "INSTALL_LOCK", false) RunUser = Cfg.MustValue("", "RUN_USER") curUser := os.Getenv("USER") if len(curUser) == 0 { curUser = os.Getenv("USERNAME") } // Does not check run user when the install lock is off. if InstallLock && RunUser != curUser { qlog.Fatalf("Expect user(%s) but current user is: %s\n", RunUser, curUser) } LogInRememberDays = Cfg.MustInt("security", "LOGIN_REMEMBER_DAYS") CookieUserName = Cfg.MustValue("security", "COOKIE_USERNAME") CookieRememberName = Cfg.MustValue("security", "COOKIE_REMEMBER_NAME") PictureService = Cfg.MustValue("picture", "SERVICE") DisableGravatar = Cfg.MustBool("picture", "DISABLE_GRAVATAR", false) // Determine and create root git reposiroty path. homeDir, err := com.HomeDir() if err != nil { qlog.Fatalf("Fail to get home directory): %v\n", err) } RepoRootPath = Cfg.MustValue("repository", "ROOT", filepath.Join(homeDir, "gogs-repositories")) if err = os.MkdirAll(RepoRootPath, os.ModePerm); err != nil { qlog.Fatalf("Fail to create RepoRootPath(%s): %v\n", RepoRootPath, err) } ScriptType = Cfg.MustValue("repository", "SCRIPT_TYPE", "bash") }
func reloadProtects(localRoot string) error { Protector.lock.Lock() defer Protector.lock.Unlock() protectPath := path.Join(localRoot, "protect.ini") if !com.IsFile(protectPath) { return nil } Protector.HasProtection = true cfgs, err := ini.Load(protectPath) if err != nil { return fmt.Errorf("Fail to load protect.ini: %v", err) } for _, k := range cfgs.Section("user").Keys() { Protector.Users[k.Name()] = strings.ToLower(k.Value()) } fmt.Println("\nProtected Resources:") for _, k := range cfgs.Section("auth").Keys() { fmt.Println("➜ ", k.Name()) Protector.Resources[k.Name()] = make(map[string]bool) for _, name := range k.Strings(",") { fmt.Println(" ✓ ", name) Protector.Resources[k.Name()][name] = true } } return nil }
// LoadConfig loads configuration file. func LoadConfig(cfgPath string) { if !com.IsExist(cfgPath) { os.Create(cfgPath) } var err error Cfg, err = goconfig.LoadConfigFile(cfgPath) if err != nil { log.Fatalf("Fail to load configuration file: %v", err) } if com.IsFile("custom/app.ini") { if err = Cfg.AppendFiles("custom/app.ini"); err != nil { log.Fatalf("Fail to load custom configuration file: %v", err) } } DocsJsPath, err = Cfg.GetValue("server", "docs_js_path") if err != nil { log.Fatalln("Fail to load configuration file: cannot find key docs_js_path") } HvJsPath, err = Cfg.GetValue("server", "hv_js_path") if err != nil { log.Fatalln("Fail to load configuration file: cannot find key hv_js_path") } }
func TestBuilderBuild(t *testing.T) { Convey("Build Process", t, func() { b.Build(target) So(b.Error, ShouldBeNil) So(b.Context(), ShouldNotBeNil) So(b.Context().Error, ShouldBeNil) // check dirs and files Convey("Check Built And Files", func() { var flag = true t := path.Join(target, b.Context().Meta.Base) for _, dir := range shoudlExistDirs { if flag = flag && com.IsDir(path.Join(t, dir)); !flag { break } } So(flag, ShouldBeTrue) for _, f := range shouldExistFiles { if flag = flag && com.IsFile(path.Join(t, f)); !flag { break } } So(flag, ShouldBeTrue) }) }) }
func getByGopmfile(ctx *cli.Context) { // Check if gopmfile exists and generate one if not. if !com.IsFile(".gopmfile") { runGen(ctx) } gf := doc.NewGopmfile(".") targetPath := parseTarget(gf.MustValue("target", "path")) // Get dependencies. imports := doc.GetAllImports([]string{workDir}, targetPath, ctx.Bool("example")) nodes := make([]*doc.Node, 0, len(imports)) for _, p := range imports { p = doc.GetProjectPath(p) // Skip subpackage(s) of current project. if isSubpackage(p, targetPath) { continue } node := doc.NewNode(p, p, doc.BRANCH, "", true) // Check if user specified the version. if v, err := gf.GetValue("deps", p); err == nil && len(v) > 0 { node.Type, node.Value = validPath(v) } nodes = append(nodes, node) } downloadPackages(ctx, nodes) doc.SaveLocalNodes() log.Log("%d package(s) downloaded, %d failed", downloadCount, failConut) }
func init() { log.Prefix = "[Go Walker]" sources := []interface{}{"conf/app.ini"} if com.IsFile("custom/app.ini") { sources = append(sources, "custom/app.ini") } var err error Cfg, err = macaron.SetConfig(sources[0], sources[1:]...) if err != nil { log.FatalD(4, "Fail to set configuration: %v", err) } if Cfg.Section("").Key("RUN_MODE").String() == "prod" { ProdMode = true macaron.Env = macaron.PROD macaron.ColorLog = false } sec := Cfg.Section("server") HTTPPort = sec.Key("HTTP_PORT").MustInt(8080) FetchTimeout = time.Duration(sec.Key("FETCH_TIMEOUT").MustInt(60)) * time.Second DocsJsPath = sec.Key("DOCS_JS_PATH").MustString("raw/docs/") DocsGobPath = sec.Key("DOCS_GOB_PATH").MustString("raw/gob/") GitHubCredentials = "client_id=" + Cfg.Section("github").Key("CLIENT_ID").String() + "&client_secret=" + Cfg.Section("github").Key("CLIENT_SECRET").String() }
func initBlogMap() { os.Mkdir("blog", os.ModePerm) langs := strings.Split(Cfg.MustValue("lang", "types"), "|") for _, l := range langs { os.Mkdir("blog/"+l, os.ModePerm) } if !com.IsFile("conf/blogTree.json") { beego.Error("models.initBlogMap -> conf/blogTree.json does not exist") return } f, err := os.Open("conf/blogTree.json") if err != nil { beego.Error("models.initBlogMap -> load data:", err.Error()) return } defer f.Close() d := json.NewDecoder(f) err = d.Decode(&blogTree) if err != nil { beego.Error("models.initBlogMap -> decode data:", err.Error()) return } blogLock.Lock() defer blogLock.Unlock() blogMap = make(map[string]*docFile) for _, v := range blogTree.Tree { blogMap[v.Path] = getFile("blog/" + v.Path) } }
func initLangDocs(tocs map[string]*Toc, localRoot, lang string) { toc := tocs[lang] for _, dir := range toc.Nodes { if err := dir.ReloadContent(); err != nil { log.Error("Fail to load doc file: %v", err) continue } for _, file := range dir.Nodes { if com.IsFile(file.FileName) { if err := file.ReloadContent(); err != nil { log.Error("Fail to load doc file: %v", err) continue } } } } for _, page := range toc.Pages { if err := page.ReloadContent(); err != nil { log.Error("Fail to load doc file: %v", err) continue } } }
func ZipDownload(ctx *middleware.Context, params martini.Params) { commitId := ctx.Repo.CommitId archivesPath := filepath.Join(ctx.Repo.GitRepo.Path, "archives") if !com.IsDir(archivesPath) { if err := os.Mkdir(archivesPath, 0755); err != nil { ctx.Handle(404, "ZipDownload -> os.Mkdir(archivesPath)", err) return } } zipPath := filepath.Join(archivesPath, commitId+".zip") if com.IsFile(zipPath) { ctx.ServeFile(zipPath, ctx.Repo.Repository.Name+".zip") return } err := ctx.Repo.Commit.CreateArchive(zipPath) if err != nil { ctx.Handle(404, "ZipDownload -> CreateArchive "+zipPath, err) return } ctx.ServeFile(zipPath, ctx.Repo.Repository.Name+".zip") }
func buildBinary(ctx *cli.Context, args ...string) { genNewGoPath(ctx, false) log.Trace("Building...") cmdArgs := []string{"go", "build"} cmdArgs = append(cmdArgs, args...) err := execCmd(newGoPath, newCurPath, cmdArgs...) if err != nil { log.Error("build", "fail to build program:") log.Fatal("", "\t"+err.Error()) } if isWindowsXP { fName := path.Base(pkgName) binName := fName + ".exe" os.Remove(binName) exePath := filepath.Join(curPath, doc.VENDOR, "src", pkgName, binName) if com.IsFile(exePath) { err = os.Rename(exePath, filepath.Join(curPath, binName)) if err != nil { log.Error("build", "fail to move binary:") log.Fatal("", "\t"+err.Error()) } } else { log.Warn("No binary generated") } } }
func InitModels() { if !com.IsFile(_CFG_PATH) { os.Create(_CFG_PATH) } var err error Cfg, err = goconfig.LoadConfigFile(_CFG_PATH) if err == nil { beego.Info("Initialize app.ini") } setGithubCredentials(Cfg.MustValue("github", "client_id"), Cfg.MustValue("github", "client_secret")) docLock = new(sync.RWMutex) // Load documentation. initDocMap() beego.RunMode = Cfg.MustValue("beego", "run_mode") // ATTENTION: you'd better comment following code when developing. if beego.RunMode == "pro" { // Start check ticker. checkTicker = time.NewTicker(5 * time.Minute) go checkTickerTimer(checkTicker.C) checkDocUpdates() } }
func (s *Server) serveFile(ctx *tango.Context, file string) bool { if com.IsFile(file) { ctx.ServeFile(file) return true } return false }
// FIXME: limit size. func UpdateAvatarSetting(ctx *middleware.Context, form auth.UploadAvatarForm, ctxUser *models.User) error { ctxUser.UseCustomAvatar = form.Enable if form.Avatar != nil { fr, err := form.Avatar.Open() if err != nil { return fmt.Errorf("Avatar.Open: %v", err) } data, err := ioutil.ReadAll(fr) if err != nil { return fmt.Errorf("ReadAll: %v", err) } if _, ok := base.IsImageFile(data); !ok { return errors.New(ctx.Tr("settings.uploaded_avatar_not_a_image")) } if err = ctxUser.UploadAvatar(data); err != nil { return fmt.Errorf("UploadAvatar: %v", err) } } else { // In case no avatar at all. if form.Enable && !com.IsFile(ctx.User.CustomAvatarPath()) { return errors.New(ctx.Tr("settings.no_custom_avatar_available")) } } if err := models.UpdateUser(ctxUser); err != nil { return fmt.Errorf("UpdateUser: %v", err) } return nil }
func (s *Server) serveFile(w http.ResponseWriter, r *http.Request, file string) bool { if com.IsFile(file) { log15.Debug("Server|Dest|%s", file) http.ServeFile(w, r, file) return true } return false }