Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
5719e437
Commit
5719e437
authored
Mar 06, 2015
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1556 from jwilder/jw-export
Add dashboards:export CLI command
parents
dba7062f
433070a1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
104 additions
and
2 deletions
+104
-2
main.go
+1
-0
pkg/cmd/dashboard.go
+103
-2
No files found.
main.go
View file @
5719e437
...
...
@@ -43,6 +43,7 @@ func main() {
cmd
.
ListOrgs
,
cmd
.
CreateOrg
,
cmd
.
DeleteOrg
,
cmd
.
ExportDashboard
,
cmd
.
ImportDashboard
,
cmd
.
ListDataSources
,
cmd
.
CreateDataSource
,
...
...
pkg/cmd/dashboard.go
View file @
5719e437
...
...
@@ -2,6 +2,7 @@ package cmd
import
(
"encoding/json"
"io"
"os"
"path/filepath"
"strings"
...
...
@@ -12,7 +13,8 @@ import (
m
"github.com/grafana/grafana/pkg/models"
)
var
ImportDashboard
=
cli
.
Command
{
var
(
ImportDashboard
=
cli
.
Command
{
Name
:
"dashboards:import"
,
Usage
:
"imports dashboards in JSON from a directory"
,
Description
:
"Starts Grafana import process"
,
...
...
@@ -23,7 +25,21 @@ var ImportDashboard = cli.Command{
Usage
:
"path to folder containing json dashboards"
,
},
},
}
}
ExportDashboard
=
cli
.
Command
{
Name
:
"dashboards:export"
,
Usage
:
"exports dashboards in JSON from a directory"
,
Description
:
"Starts Grafana export process"
,
Action
:
runExport
,
Flags
:
[]
cli
.
Flag
{
cli
.
StringFlag
{
Name
:
"dir"
,
Usage
:
"path to folder containing json dashboards"
,
},
},
}
)
func
runImport
(
c
*
cli
.
Context
)
{
dir
:=
c
.
String
(
"dir"
)
...
...
@@ -104,3 +120,88 @@ func importDashboard(path string, orgId int64) error {
return
nil
}
func
runExport
(
c
*
cli
.
Context
)
{
initRuntime
(
c
)
if
!
c
.
Args
()
.
Present
()
{
log
.
ConsoleFatal
(
"Account name arg is required"
)
}
name
:=
c
.
Args
()
.
First
()
orgQuery
:=
m
.
GetOrgByNameQuery
{
Name
:
name
}
if
err
:=
bus
.
Dispatch
(
&
orgQuery
);
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to find organization: %s"
,
err
)
}
orgId
:=
orgQuery
.
Result
.
Id
dir
:=
c
.
String
(
"dir"
)
dash
:=
c
.
Args
()
.
Get
(
1
)
query
:=
m
.
SearchDashboardsQuery
{
OrgId
:
orgId
,
Title
:
dash
}
err
:=
bus
.
Dispatch
(
&
query
)
if
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to find dashboards: %s"
,
err
)
return
}
if
dir
==
""
&&
len
(
query
.
Result
)
>
1
{
log
.
ConsoleFatalf
(
"Dashboard title '%s' returned too many results. "
+
"Use --dir <dir> or a more specific title"
,
dash
)
return
}
for
_
,
v
:=
range
query
.
Result
{
f
:=
os
.
Stdout
if
dir
!=
""
{
dest
:=
filepath
.
Join
(
dir
,
v
.
Slug
+
".json"
)
f
,
err
=
os
.
Create
(
dest
)
if
err
!=
nil
{
log
.
ConsoleFatalf
(
"Unable to create file: %s"
,
err
)
}
log
.
ConsoleInfof
(
"Exporting '%s' dashboard to %s"
,
v
.
Title
,
dest
)
}
exportDashboard
(
f
,
orgId
,
v
.
Slug
)
if
dir
!=
""
{
if
err
:=
f
.
Sync
();
err
!=
nil
{
log
.
ConsoleFatalf
(
"Unable to sync file: %s"
,
err
)
}
if
err
:=
f
.
Close
();
err
!=
nil
{
log
.
ConsoleFatalf
(
"Unable to close file: %s"
,
err
)
}
}
}
if
dir
!=
""
{
log
.
ConsoleInfof
(
"Exported %d dashboards to %s"
,
len
(
query
.
Result
),
dir
)
}
}
func
exportDashboard
(
w
io
.
Writer
,
orgId
int64
,
slug
string
)
{
query
:=
m
.
GetDashboardQuery
{
Slug
:
slug
,
OrgId
:
orgId
}
err
:=
bus
.
Dispatch
(
&
query
)
if
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to find dashboard: %s"
,
err
)
return
}
out
,
err
:=
json
.
MarshalIndent
(
query
.
Result
.
Data
,
""
,
" "
)
if
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to marshal dashboard: %s"
,
err
)
return
}
n
,
err
:=
w
.
Write
(
out
)
if
err
!=
nil
{
log
.
ConsoleFatalf
(
"Failed to write dashboard: %s"
,
err
)
return
}
if
n
!=
len
(
out
)
{
log
.
ConsoleFatalf
(
"Failed to write dashboard: wrote %d expected %d"
,
n
,
len
(
out
))
return
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment